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 ).
Related
I need to get specific columns from three tables through joins.every time it goes wrong.my code is
$saf=mysqli_query($db , "select pod.mobile, tpaidamount.Aptdate, follow.cent, pdate, time from pod,tpaidamount, follow where tpaidamount.pid = follow.pid and pod.Customer Id = tpaidamount.pid and pod.Customer Id =follow.pid ");
$i=1;
while($sfg=mysqli_fetch_assoc($saf) or die(mysqli_error($db)))
;
?>
pod,tpaidamount,follow are tables and other coloumns
Getting 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 'Id = tpaidamount.pid and pod.Customer Id =follow.pid' at line 1
is it a typo? Is setfollowup.pid or follow.pid?
select pod.mobile, tpaidamount.Aptdate, follow.cent, <table>.pdate, <table>.time
from pod, tpaidamount, follow
where tpaidamount.pid = follow.pid
and pod.`Customer Id` = tpaidamount.pid
and pod.`Customer Id` = follow.pid
You shouldn't Ever create columns names with spaces. The name: "pod.Customer Id" is a Bad attribute name, and you need to avoid use names like datatypes (or any SGBD reserved word) like: 'date', 'time', 'char', 'table', 'column'....
However, if you need to do it, try this SQL:
SELECT p.mobile
, t.Aptdate
, f.cent
, ???.pdate
, ???.`time`
FROM pod AS p
JOIN tpaidamount AS t ON o.`Customer Id` = t.pid
JOIN follow AS f ON t.pid = f.pid ON p.`Customer Id` = f.pid
Use alias for easy coding SQL queryes. Ex: table_name AS tn, table_a_name AS tan.
!!! I sugest you to watch some basic SQL Lessons.
Good Luck.
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 ...";
My SQL query is SELECT * FROM chat WHERE to = '$user_id' AND client_id = '001' LIMIT 4
For some reason that query gives me the following 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 'to = '1' AND client_id = '001' LIMIT 4' at line 1
I used a different row and the query ran perfectly fine - is the error because of the word "to"? Or is there something else behind this?
Just for your reference, here's the PHP:
$user_id = $_SESSION['user_id'];
$client_id = '001';
if (!$query = sql("SELECT * FROM arrowchat WHERE to = '$user_id' AND client_id = '$client_id' LIMIT 4")) {
echo mysql_error();
} else {
echo 'success';
}
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
to is a reserved keyword in mysql, you cannot use it as is in a query. You need to wrap it in backticks:
SELECT * FROM chat WHERE `to` = '$user_id' AND client_id = '001' LIMIT 4
to is reserved mysql keyword you need to use backticks like that :
SELECT * FROM arrowchat WHERE `to` ....
reserved keywords
I'm trying to use the answer given at : How to get ID of the last updated row in MySQL?
I want the ID of the row which was updated. But the following doesn't work giving the error
Fatal error: Call to a member function fetch_assoc() on a non-object.
$query = "
SET #update_id := 0;
UPDATE locations SET owner_player_id='$player_id', id=(SELECT #update_id := id)
WHERE game_id='$game_id' LIMIT 1;
SELECT #update_id;
";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
$location_id = $row['update_id'];
Thanks
Edit:
$mysqli->error gives 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 'UPDATE locations SET owner_player_id='5', id=(SELECT #update_id := id) WHERE gam' at line 1
Your SQL has failed, that is why your subsequent fetch_assoc() method is also failing.
You will want to look into running your query without using the variables (#update_id) as you are intending, and look at using mysqli::$insert_id.
Without knowing what it is you are trying to do exactly, it's hard to be more specific.
Have to use mysqli::multi_query not mysqli::query. Also then have to use mysqli::next_result and mysqli::store_result to get the correct data.
$query = "SET #update_id := 0; UPDATE locations SET owner_player_id='$player_id', id = ( SELECT #update_id := id ) WHERE game_id='$game_id' LIMIT 1; SELECT #update_id;";
$mysqli->multi_query($query) or die($mysqli->error);
$mysqli->next_result();
$mysqli->next_result();
$result = $mysqli->store_result();
$row = $result->fetch_assoc();
$location_id = $row['#update_id'];
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'");