SELECT, JOIN, and a WHERE != statement returning too many rows

Chuck

Trying to get a tricky mysql select statement working - and I need some help to cool off my burning noggin...and I have a feeling one of you MYSQL heroes out there will look at this and reel it off.

Goal: List a given user's songs NOT in a given category.

Three tables: : table 1: song, many fields with assigned UserID, and unique SongID

table 2: category, 3+ fields, with assigned UserID and unique CatID

table 3: linker, one-to-many for listing songs in one or more categories. 3 fields, unique id (index), SongID, CatID

The following gets me close - but does not list a user's songs that aren't assigned to any other category at all OR are a already assigned to a another category (I think thanks to !=).

SELECT DISTINCT song.SongName, song.UserID, song.SongID  FROM song  
JOIN linker ON song.SongID = linker.SongID 
WHERE  linker.CatID != 155 AND song.UserID =2

Then, I tried this,

SELECT DISTINCT song.SongName, song.UserID, song.SongID FROM song 
LEFT JOIN linker ON song.SongID = linker.SongID 
WHERE (linker.SongID IS NULL OR linker.CatID != 155) AND song.UserID =2 

Closer but not working (still thanks to != including songs already assigned).

I was thinking I can get away without invoking table 2, since it merely adds and defines categories for a given user. Alternatively, I'm thinking of getting all the user's songs, and then unsetting array values with a given CatID - but this doesn't seem like it should be necessary? I feel like I'm missing something simple? Table structure is not sacred at this point if it absolutely needs to change. Thanks to any who share their thoughts.

Becuzz

Try this (I am used to MSSQL so if my syntax is off, appologies in advance):

SELECT s.SongName, s.UserID, s.SongID
FROM song s
LEFT JOIN linker l on s.SongID = l.SongID AND l.CatID = 155
WHERE s.UserID = 2
AND l.ID is null

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redshift Query returning too many rows in aggregate join

From Dev

Case subquery returning too many rows within where clause

From Dev

WHERE returning too many rows for data NOT BETWEEN two dates

From Dev

Adding an extra SQL select statement is returning too many results

From Dev

Where clause returning too many results

From Dev

MYSQL delete statement deletes too many rows

From Dev

SqlDataReader not returning any rows when I use a select statement with a where clause

From Dev

SQLITE3 Inner Join Returning Too Many Tuples

From Dev

google big query limit clause returning too many rows

From Dev

How many rows are selected in the select in this update statement

From Dev

MySQL JOIN returning unrelated rows when combined with LEFT JOIN, WHERE and OR

From Dev

Optimize query contains too many inner join and rows

From Dev

updating multiple rows with select..where..in and a join

From Dev

MySQL select rows where left join is null

From Dev

too many arguments in if statement

From Dev

Codeigniter: Query is returning all rows in table on select/like statement

From Dev

Select Statement in Where Clause Returning More Than 1 Value

From Dev

Returning specific Group By Criteria from a Select Statement Inside of a Where Clause

From Dev

Natural Join not returning rows

From Dev

SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

From Dev

SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

From Dev

SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

From Dev

SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

From Dev

serializeArray() returning too many elements

From Dev

Is it possible to create many rows on a select statement without table?

From Dev

LEFT JOIN on SELECT AS alias causing column not found on WHERE statement

From Dev

Comparing too many variables in if statement

From Dev

php too many if else statement

From Dev

"too many arguments in [ (test) statement"

Related Related

  1. 1

    Redshift Query returning too many rows in aggregate join

  2. 2

    Case subquery returning too many rows within where clause

  3. 3

    WHERE returning too many rows for data NOT BETWEEN two dates

  4. 4

    Adding an extra SQL select statement is returning too many results

  5. 5

    Where clause returning too many results

  6. 6

    MYSQL delete statement deletes too many rows

  7. 7

    SqlDataReader not returning any rows when I use a select statement with a where clause

  8. 8

    SQLITE3 Inner Join Returning Too Many Tuples

  9. 9

    google big query limit clause returning too many rows

  10. 10

    How many rows are selected in the select in this update statement

  11. 11

    MySQL JOIN returning unrelated rows when combined with LEFT JOIN, WHERE and OR

  12. 12

    Optimize query contains too many inner join and rows

  13. 13

    updating multiple rows with select..where..in and a join

  14. 14

    MySQL select rows where left join is null

  15. 15

    too many arguments in if statement

  16. 16

    Codeigniter: Query is returning all rows in table on select/like statement

  17. 17

    Select Statement in Where Clause Returning More Than 1 Value

  18. 18

    Returning specific Group By Criteria from a Select Statement Inside of a Where Clause

  19. 19

    Natural Join not returning rows

  20. 20

    SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

  21. 21

    SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

  22. 22

    SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

  23. 23

    SQL LEFT JOIN with WHERE statement after is not displaying unconnected rows

  24. 24

    serializeArray() returning too many elements

  25. 25

    Is it possible to create many rows on a select statement without table?

  26. 26

    LEFT JOIN on SELECT AS alias causing column not found on WHERE statement

  27. 27

    Comparing too many variables in if statement

  28. 28

    php too many if else statement

  29. 29

    "too many arguments in [ (test) statement"

HotTag

Archive