dbTalk Databases Forums  

MySQL Statement

comp.databases.mysql comp.databases.mysql


Discuss MySQL Statement in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joe
 
Posts: n/a

Default MySQL Statement - 03-22-2011 , 02:05 PM






Hello,

I am trying to get a list from one table, and using another table to
subtract from that list. I have tried all sorts of versions, but just
can't hit on the right one.

I am trying to use:

SELECT entry_id, entry.video_id, entry_book, entry_category, sort_cat,
entry_guard_fname, entry_guard_lname, entry_fname, entry_lname
FROM entry, chosen
where sort_cat = 2
and entry.video_id in
(Select video_id from chosen where chosen_win = "")

Which gives me way too may results, 4,042.

The first part, comes up with 157 records, where the second part is
only 5. I want the final list to be 152.

I have hit a wall here, and not sure where to go next. Can anyone
help?

Reply With Quote
  #2  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: MySQL Statement - 03-22-2011 , 02:15 PM






Joe:

Quote:
Hello,

I am trying to get a list from one table, and using another table to
subtract from that list. I have tried all sorts of versions, but just
can't hit on the right one.

I am trying to use:

SELECT entry_id, entry.video_id, entry_book, entry_category, sort_cat,
entry_guard_fname, entry_guard_lname, entry_fname, entry_lname
FROM entry, chosen
where sort_cat = 2
and entry.video_id in
(Select video_id from chosen where chosen_win = "")

Which gives me way too may results, 4,042.

The first part, comes up with 157 records,
What do you consider "the first part"?

Quote:
where the second part is only 5.
Would that be the number of rows from
"Select video_id from chosen where chosen_win = ''"?


Quote:
I want the final list to be 152.
Well, if you DON'T want the rows from the latter subquery, you should
specify ...and entry.video_id NOT IN....

But that wouldn't explain why you get so many results.

Reply With Quote
  #3  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: MySQL Statement - 03-23-2011 , 02:30 AM



El 22/03/2011 21:05, Joe escribió/wrote:
Quote:
I am trying to get a list from one table, and using another table to
subtract from that list. I have tried all sorts of versions, but just
can't hit on the right one.

I am trying to use:

SELECT entry_id, entry.video_id, entry_book, entry_category, sort_cat,
entry_guard_fname, entry_guard_lname, entry_fname, entry_lname
FROM entry, chosen
IMHO, this join syntax is terribly confusing. You have to mix the join
conditions and the query filters in the WHERE clause and you can never
be 100% sure of how tables are getting joined.

Quote:
where sort_cat = 2
and entry.video_id in
(Select video_id from chosen where chosen_win = "")
You are already fetching data from `chosen` in the main query :-?

Quote:
Which gives me way too may results, 4,042.

The first part, comes up with 157 records, where the second part is
only 5. I want the final list to be 152.

I have hit a wall here, and not sure where to go next. Can anyone
help?
Not being sure of what you want to accomplish, your description suggests
you want something on this line:

SELECT entry_id, en.video_id, entry_book, entry_category, sort_cat,
entry_guard_fname, entry_guard_lname, entry_fname, entry_lname
FROM entry en
LEFT JOIN chosen ch ON en.video_id=ch.video_id
WHERE sort_cat=2
AND ch.chosen_win IS NULL

This also assumes that chosen_win is set to NULL when it doesn't have a
value, rather than empty strings (a fairly non-standard way to store
blank data).


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.