how is the syntax of using NOT EXISTS in mysql? - mysql

I got error message :
Query 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 'AS ChildCnt from tbl_user_master u WHERE u.sponsor_id = '145' AND u.user_type = ' at line 4 - Invalid query: select u.user_id,(SELECT COUNT(o.user_id) FROM tbl_user_master o WHERE o.sponsor_id = u.user_id AND o.user_type = 2 AND o.status =1 AND NOT EXISTS (
mysql code :
$user_tree = $this->getAllRec("u.user_id,(SELECT COUNT(o.user_id) FROM tbl_user_master o WHERE o.sponsor_id = u.user_id AND o.user_type = 2 AND o.status = 1 AND NOT EXISTS (
SELECT p.user_id
FROM tbl_flamingo_product_order p
WHERE o.user_id = p.user_id AND p.status=3) AS ChildCnt",
"tbl_user_master u",
"WHERE u.sponsor_id = '".$user_id."' AND u.user_type = 2 AND u.status =1");
May you help me find out where is the syntax error?

it is ok. I found it.
just replace
status=3)
to
status=3))

Related

What is the correct syntax?

I wrote a sql query to update data of 'tbl_products'.
Here is the query
update tbl_products
set product_count = (product_count - tbl_order_details.product_sales_quantity)
from tbl_products
join tbl_order_details on tbl_order_details.product_id = tbl_products.product_id
join tbl_order on tbl_order.order_id = tbl_order_details.order_id
where tbl_order.order_id = 54;
But it gives me the following error
"#1064 - 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 'from tbl_products join tbl_order_details on tbl_order_details.product_id = tbl_p' at line 1"
Whats wrong here?
In MySQL, the correct syntax is:
update tbl_products p join
tbl_order_details od
on od.product_id = p.product_id join
tbl_order o
on o.order_id = od.order_id
set p.product_count = (p.product_count - od.product_sales_quantity)
where o.order_id = 54;

MYSQL Update Query syntax error 28-10-2016a

UPDATE DFEntryValues
SET DFEntryValues.DFFieldvalue = NOW()
FROM DFEntryValues
JOIN DFEntries ON DFEntryValues.DFEntryID = DFEntries.DFEntryID
JOIN DynamicFormStructures ON DFEntries.DynamicFormStructureID = DynamicFormStructures.DynamicFormStructureID
JOIN Projects ON DynamicFormStructures.ProjectID = Projects.ProjectId
JOIN Clients ON Projects.ClientID = Clients.ClientID
JOIN DFFieldDefinition ON DFEntryValues.DFFieldDefinitionID = DFFieldDefinition.DFFieldDefinitionID
WHERE Clients.ClientID = '26' AND DFFieldDefinition.label = 'Geboortedatum';
I get the following error:
Error Code: 1064
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 Clients.ClientID = '26' AND DFFieldDefinition.label = 'Geboortedatum' SET' at line 12
Can someone point me out what's wrong with this query?
Kind regards!!
It seems you are not using correct format.
Hope this helps.
UPDATE DFEntryValues JOIN DFEntries ON DFEntryValues.DFEntryID = DFEntries.DFEntryID
JOIN DynamicFormStructures ON DFEntries.DynamicFormStructureID = DynamicFormStructures.DynamicFormStructureID
JOIN Projects ON DynamicFormStructures.ProjectID = Projects.ProjectId
JOIN Clients ON Projects.ClientID = Clients.ClientID
JOIN DFFieldDefinition ON DFEntryValues.DFFieldDefinitionID = DFFieldDefinition.DFFieldDefinitionID
SET DFEntryValues.DFFieldvalue = NOW()
WHERE Clients.ClientID = '26' AND DFFieldDefinition.label = 'Geboortedatum';

Error received on join update, incorrect syntax

I'm receiving an error on the below SQL in MySQL and i'm unsure why. I'm new to MySQL.
UPDATE I
SET I.user_comments = DD.value
FROM
redcap_user_information I
JOIN redcap_data D ON D.value = I.user_email AND D.project_id = 439 AND D.field_name = 'email'
JOIN redcap_data DD ON DD.record = D.record AND DD.project_id = 439 AND DD.field_name = 'irb'
WHERE
user_comments IS NULL
AND allow_create_db = 1
AND user_suspended_time IS NULL
ORDER BY I.user_email
The error is
Error Code: 1064. 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 'FROM
redcap_user_information I
JOIN redcap_data D ON D.value = I.use'
Can anyone shed some light?
Not sure about the logic of the join part but the correct syntax for update with join would be as
update redcap_user_information I
JOIN redcap_data D ON D.value = I.user_email AND D.project_id = 439 AND D.field_name = 'email'
JOIN redcap_data DD ON DD.record = D.record AND DD.project_id = 439 AND DD.field_name = 'irb'
set I.user_comments = DD.value
WHERE
user_comments IS NULL --- use the table alias name to access the column
AND allow_create_db = 1 --- use the table alias name to access the column
AND user_suspended_time IS NULL --- use the table alias name to access the column

UPDATE and JOIN causing a Syntax Error in MySQL

UPDATE default_weekly_stats s
INNER JOIN default_profiles p
ON p.user_id = s.user_id
WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL)
AND s.week = `1`
AND s.correct_picks = `4`
SET s.rank = 1
That's my query and I'm getting an 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 (default_profiles.opid = 0 OR default_profiles.opid IS NULL) AND s.week = ' at line 1
The syntax looks right to me, but clearly I'm missing something. Any ideas what?
WHERE comes after SET
UPDATE default_weekly_stats s
INNER JOIN default_profiles p ON p.user_id = s.user_id
SET s.rank = 1
WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL)
AND s.week = 1
AND s.correct_picks = 4
And as #Chad mentioned: Leave the backticks. You could use quotes but don't need any delimiter for numbers.
Look at the UPDATE Syntax
Try this:
UPDATE
default_weekly_stats s
INNER JOIN default_profiles p
ON p.user_id = s.user_id
SET s.rank = 1
WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL)
AND s.week = `1`
AND s.correct_picks = `4`

I am getting mysql error check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 4

I am getting my sql 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 '?' at line 4
SELECT cm.user_id, chat_message_content, u.firstname FROM chat_message cm JOIN user u ON cm.user_id = u.user_id WHERE cm.chat_id= ?"
Model
function get_chat_messages($chat_id) {
$query_str = "SELECT cm.user_id, chat_message_content, u.firstname
FROM chat_message cm
JOIN user u ON cm.user_id = u.user_id
WHERE cm.chat_id= ?";
$results = $this->db->query($query_str, $chat_id);
return $results;
}
Try putting quotes around your question mark if it's the value in the database you're filtering by:
$query_str = "SELECT cm.user_id, chat_message_content, u.firstname
FROM chat_message cm
JOIN user u ON cm.user_id = u.user_id
WHERE cm.chat_id= '?'";
Try the following:
$query_str = "SELECT cm.user_id, chat_message_content, u.firstname
FROM chat_message cm
JOIN user u ON cm.user_id = u.user_id
WHERE cm.chat_id= (?)";