Getting values from different tables in mysql query (practical example) - php

I have two tables: first and second
this and that are primary keys, common and always present on both tables, so I guess there is no need of left joins
$query = "SELECT
first.one,first.going,first.what,first.ever,second.another,second.outre,second.oneplus,second.more,second.anotherthing,second.alldifferent
WHERE second.THIS = first.THAT AND first.is = '1' AND
first.yet = '$variable' AND second.againe = '1'";
The error is the following
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE second.THIS = first.THAT AND first.is = '1' AND first.y' at line 1
But I can't get to understand why this happens.
Any help on this one? Ty very much

You need to specify a table name.
$query = "SELECT first.one, ... ,second.alldifferent WHERE ...";
Should be
$query = "SELECT first.one, ... ,second.alldifferent FROM first, second WHERE ...";

Related

Mysqli query with zerofill

Hello stackoverflow community, I need help with this mysqli query. My auto increase column in database is ZERO-FILL element for example 00001 so when I try to query like this:
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '".$this->clean_k_name."',
pavard = ".$this->clean_k_surname.",
WHERE korteles_nr = '00001'");
And I get error:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'WHERE korteles_nr = 00001' at line 18
I was searching Internet for solving this problem. But can't find so please help!
Query should be like this:-
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '$this->clean_k_name',
pavard = '$this->clean_k_surname'
WHERE
korteles_nr = '00001'");
You have to remove the comma after $this->clean_k_surname variable

right syntax to use near '%+ads.title+%\r\n AND\r\n products_request

I would like to compare two columns by like :
$sql = "SELECT
products_requests.id as req_id,ads.id as send_id,
products_requests.user_id,ads.des,ads.title
FROM
products_requests,ads
WHERE
products_requests.title LIKE '%'+ads.title+'%'
AND
products_requests.ads_cat_id = ads.ads_cat_id
AND
products_requests.ads_sub_cat_id = ads.ads_sub_cat_id
AND
products_requests.price_type_id = ads.price_type_id
AND
products_requests.province_id = ads.province_id
AND
products_requests.city_id = ads.city_id";
but I get this error :
"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '%+ads.title+%\r\n AND\r\n products_request' at line 9"
In MySQL, perform string concatenation operations with the CONCAT function
... LIKE CONCAT('%',ads.title,'%')
I also recommend you ditch the old-school comma syntax for the join operation, and use the JOIN keyword instead. And move the join predicates into an ON clause. And consider using shorter table aliases to qualify the column references.
SELECT req.id AS req_id
, ads.id AS send_id
, req.user_id
, ads.des
, ads.title
FROM products_requests req
JOIN ads
ON req.title LIKE CONCAT('%',ads.title,'%')
AND req.ads_cat_id = ads.ads_cat_id
AND req.ads_sub_cat_id = ads.ads_sub_cat_id
AND req.price_type_id = ads.price_type_id
AND req.province_id = ads.province_id
AND req.city_id = ads.city_id

PHP Mysqli select two DISTINCT from data base

I am trying to fetch two different DISTINCT values from data base am not able to do that that gives me an error. I don't want both to be combined in as statement and fetch as one I need individual DISTINCT values.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT AttendanceID from attendance_master where Attendence_taken_by !='admi' at line 1\
My Query
$query = "select DISTINCT Standard , DISTINCT AttendanceID from attendance_master where Attendence_taken_by !='$admin' and SchoolID='$schoolid' and AttendanceDate >= '$tfrom_date' and AttendanceDate <= '$tto_date' ";
$result = mysqli_query($mysqli,$query)or die(mysqli_error($mysqli));
while($row=mysqli_fetch_array($result))
{
echo "<table><tr>".$row["Standard"]."</tr><tr>".$row["AttendanceID"]."</tr></table>";
}
Syntax of DISTINCT is
SELECT DISTINCT column_name,column_name
FROM table_name;
So change your query with
$query = "select DISTINCT Standard , AttendanceID from attendance_master.....
Instead of assign direct value use bind_param

Mysql query for using count on a view in php

I have a query:
$result = mysql_query("CREATE VIEW temporary(IngList) AS (
SELECT DISTINCT (r1.Ingredient)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'
AND r1.Ingredient = r2.Ingredient)
SELECT COUNT(*) FROM temporary");
I want the query to make a view called temporary and have it return a count of the number of rows in the view temporary. I know this code works without the SELECT COUNT(*) because I checked my database and the view is created.
Yet this code throws the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT(*) FROM temporary' at line 1
I checked the syntax and it seems to be correct. What seems to be the problem because its quite frustrating.
From the mysql_query documentation:
mysql_query() sends a unique query (multiple queries are not supported)...
You can't create the view, and select from it in a single mysql_query. The view is unnecessary:
$sql = sprintf("SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
WHERE r.country = '%s'
AND EXISTS(SELECT NULL
FROM recipes r2
WHERE r2.Country = '%s'
AND r1.Ingredient = r2.Ingredient)",
$temp, $temp2);
$result = mysql_query($sql);
For starters you have two statements. What you're writing looks more like a stored procedure. Even if it worked, you would need a semicolon at the end of the first statement. And another statement somewhere saying "DROP VIEW ...." when you are done.
And a temp view is a bit of a non sequitur. I can't find any reference to "CREATE VIEW temporary". Or maybe it's to create a view named temporary with an argument? Views don't take arguments.
I think you might get what you want with a semi-simple SQL statement something like:
$result = mysql_query(
"SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
JOIN recipes r2 ON r1.Ingredient = r2.Ingredient
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'");

Sql syntax error

Here is my query:
$query="Delete b
Where Exists
(
Select 1
From a
Where a.poster_password = '$pass'
And a.ad_id = '$id'
And a.classified_id = b.classified_id
)
Delete a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";
I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Where Exists ( Select 1 From a Where a.poster_p' at line 2
If you need more input let me know...
Whats wrong here?
Thanks
UDPATE:
Just a Q: Do I need to specify also that a = "this table" and b = "another table" or does MySql get that by this code?
As for the new code posted where to use FROM and a terminator semicolon, wont work and give this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Delete FROM a Where a.poster_password = 'xxxxxxxxxxxxxxxxxxxxx' at line 10
UPDATE2:
$query="Delete FROM $sql_table
Where Exists
(
Select 1
From classified
Where classified.poster_password = '$pass'
And classified.ad_id = '$id'
And classified.classified_id = $sql_table.classified_id
);
Delete FROM classified
Where classified.poster_password = '$pass'
And classified.ad_id = '$id'";
And when I echo $query: (fordon is in this case $sql_table variable.)
Delete FROM fordon
Where Exists
(
Select 1
From classified
Where classified.poster_password = 'xxxxx'
And classified.ad_id = 'motorbat_166250627'
And classified.classified_id = fordon.classified_id
);
Delete FROM classified
Where classified.poster_password = 'xxxxx'
And classified.ad_id = 'motorbat_166250627'
Thanks again
You're not specifying the tables to delete from. Try:
$query="Delete FROM b
Where Exists
(
Select 1
From a
Where a.poster_password = '$pass'
And a.ad_id = '$id'
And a.classified_id = b.classified_id
);
Delete FROM a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";
I've also added in a semicolon after the end of the first DELETE query. If you want to run both at the same time, you'll need a separator to terminate the first query, before you run the second version.
Re. your question edit about MySQL "getting" the tables - if a and b are aliases here, then no, MySQL doesn't know what a and b are. You'll need to alias the tables, or replace a and b with the actual table names.
The two deletes need to be separate statements ( and executed separately ).

Categories