rockinaway Posted February 16, 2008 Share Posted February 16, 2008 I have several left joins to my SQL query.. and one of the LEFT joins will return several rows. For that specific left join I want to only select the first row. How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/ Share on other sites More sharing options...
toplay Posted February 16, 2008 Share Posted February 16, 2008 It's always best to post the query you have when posting on this forum and be more specific. Show sample data of what you have and what you'd like to see. Choices: narrow where clause, sub query, group by, distinct, limit ... just depends on exactly what's needed. Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468440 Share on other sites More sharing options...
rockinaway Posted February 16, 2008 Author Share Posted February 16, 2008 SELECT c.name AS cname, b.*, t.title, m.name, m.gid, g.colour, u.id AS u_id FROM test_c AS c LEFT JOIN test_m AS m ON t.id = m.id LEFT JOIN test_g AS g ON m.gid = g.gid LEFT JOIN test_lu AS u ON u.id = b.id ORDER BY c.p ASC, b.p ASC The LEFT JOIN test_lu AS u ON u.id = b.id, returns several rows. I just want the first row, when the times are put into DESC order. So I guess a WHERE and LIMIT need to be used.. but I am not sure how.. Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468441 Share on other sites More sharing options...
toplay Posted February 16, 2008 Share Posted February 16, 2008 SELECT c.name AS cname, b.*, t.title, m.name, m.gid, g.colour, u.id AS u_id FROM test_c AS c LEFT JOIN test_m AS m ON t.id = m.id LEFT JOIN test_g AS g ON m.gid = g.gid LEFT JOIN test_lu AS u ON u.id = b.id ORDER BY c.p ASC, b.p ASC The LEFT JOIN test_lu AS u ON u.id = b.id, returns several rows. I just want the first row, when the times are put into DESC order. So I guess a WHERE and LIMIT need to be used.. but I am not sure how.. Your query doesn't make any sense since I see an alias "b" that's not tied to any table. I just see alias "c", "m", "g", "u", but no "b". Here I assume that you meant c.id and not b.id: ... LEFT JOIN test_g AS g ON m.gid = g.gid LEFT JOIN (SELECT id FROM test_lu WHERE id = c.id ORDER BY a_column DESC LIMIT 1) AS u ON u.id = c.id ORDER BY ... But since you didn't display any sample data and what you want to retrieve, it's hard to say. You might want to rethink the whole query. When I see too many left joins, something is usually a miss. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468446 Share on other sites More sharing options...
rockinaway Posted February 16, 2008 Author Share Posted February 16, 2008 Thanks, I tried it but I get 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 '(SELECT id FROM test_lu WHERE id = c.id ORDER BY time DES' at line 7 SELECT c.name AS cname, c.id, t.title, m.name, m.gid, g.colour, u.id AS u_id FROM test_c AS c LEFT JOIN test_m AS m ON t.id = m.id LEFT JOIN test_g AS g ON m.gid = g.gid LEFT JOIN test_lu (SELECT id FROM test_lu WHERE id = c.id ORDER BY a_column DESC LIMIT 1) AS u ON u.id = c.id ORDER BY c.p ASC, b.p ASC That is what I am using :S Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468467 Share on other sites More sharing options...
rockinaway Posted February 16, 2008 Author Share Posted February 16, 2008 Fixed it Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468470 Share on other sites More sharing options...
rockinaway Posted February 16, 2008 Author Share Posted February 16, 2008 Actually it isn't fixed.. You see.. I have 4 main ids.. Each id has several pieces of info stored in the other table. With my current query, I get all the pieces of info, making some ids have more than others. I just want to select ONE piece of information from the table for that id. How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468487 Share on other sites More sharing options...
rockinaway Posted February 16, 2008 Author Share Posted February 16, 2008 Any help? Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468509 Share on other sites More sharing options...
rockinaway Posted February 17, 2008 Author Share Posted February 17, 2008 Bump..Anyone? Please.. Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-468743 Share on other sites More sharing options...
fenway Posted February 19, 2008 Share Posted February 19, 2008 Maybe you can explain what you want again... I'm not sure from the above what went wrong. Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-470687 Share on other sites More sharing options...
rockinaway Posted February 19, 2008 Author Share Posted February 19, 2008 No worries.. fixed it Quote Link to comment https://forums.phpfreaks.com/topic/91425-select-only-one-row-with-your-left-join/#findComment-470697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.