If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Exists -- ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-24-04, 03:09
shawnlui shawnlui is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Exists -- ?

Select field1 from table1 where exists (select fields1from table2);

The above SQL generates error: "#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 'EXISTS( SELECT field1 FROM table2 ) LIMIT 0, 30' at line 3

But the sql is an example in MySQL manual and it passed the test on MS ACCESS. I also tried to change 'exists' to
'field1 in' or 'field1 = any' or 'fields is ', none of them works.

Any idea?

Shawn
Reply With Quote
  #2 (permalink)  
Old 06-24-04, 06:37
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Depending on which version of MySQL you are using, the EXISTS clause may not be supported. In almost any version you could use:
Code:
SELECT a.field1
   FROM table1 AS a
   INNER JOIN table2 AS b ON (b.field1 = a.field1)
   LIMIT 0, 30;
-PatP
Reply With Quote
  #3 (permalink)  
Old 06-24-04, 06:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
don't forget to add DISTINCT to pat's query when there's a one-to-many relationship between table1 and table2

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 06-24-04, 09:05
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Good point! Thanks Rudy.

-PatP
Reply With Quote
  #5 (permalink)  
Old 06-24-04, 11:34
shawnlui shawnlui is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
re:

Thank you so much to both of you. Your query works. What is the meaning of 'limit 0, 30'?

BTW, the sql actually has DISTINCT but I removed it for asking the question.

thanks again.
Shawn
Reply With Quote
  #6 (permalink)  
Old 06-24-04, 11:37
shawnlui shawnlui is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
I know the meaning of 'limit 0, 30' now.

thanks;
Shawn
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On