Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
SELECT u.Id as UserId,u.Nikname as Nikname,u.surName,s.FormId,s.oldId,s.Mess,s.Date,s.Id as FrmId,s.pic  FROM db.user as u, db.`dbo.stud` as s WHERE u.Id IN(SELECT MId as Id  FROM db.`dbo.Common` WHERE studId='1' AND studStatus=1 UNION SELECT studId as Id FROM db.`dbo.stud` WHERE Id='1' AND studStatus=1));



I used this MySql query but it give an error.
please help me make this query error free

What I have tried:

C#
SELECT u.Id as UserId,u.Nikname as Nikname,u.surName,s.FormId,s.oldId,s.Mess,s.Date,s.Id as FrmId,s.pic  FROM db.user as u, db.`dbo.stud` as s WHERE u.Id IN(SELECT MId as Id  FROM db.`dbo.Common` WHERE studId='1' AND studStatus=1 UNION SELECT studId as Id FROM db.`dbo.stud` WHERE Id='1' AND studStatus=1));



I used this MySql query but it give an error.
please any me make this query error free
Posted
Updated 20-Apr-16 5:25am
v2
Comments
CHill60 20-Apr-16 11:04am    
Do you want to give us a clue what the error was?
Kishor-KW 20-Apr-16 11:07am    
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 '' at line 1

in mysql wrokbench 5.5
CHill60 20-Apr-16 11:12am    
It could be the characters you are using to surround your table names - try changing them to a simple single-quote mark '.
If that doesn't work then try running each of the sub-queries separately to see which area of the query is at fault
Kishor-KW 20-Apr-16 11:15am    
both query working well individually query at start and queries in ( )

1 solution

The problem is here
SQL
AND studStatus=1));
There should only be one closing bracket
SQL
AND studStatus=1);

Whitespace can be your friend ... I pasted your query into SQLFiddle and inserted some linefeeds ...
SQL
SELECT u.Id as UserId,u.Nikname as Nikname,u.surName,
s.FormId,s.oldId,s.Mess,s.Date,s.Id as FrmId,s.pic  
FROM user as u, `stud` as s 
WHERE u.Id IN (
  SELECT MId as Id  FROM `Common` 
  WHERE studId='1' AND studStatus=1 
  UNION 
  SELECT studId as Id FROM `stud` WHERE Id='1' AND studStatus=1)
  );

The error message then said the error was on Line 9 ... straight to the problem!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900