ScopeXL Posted September 17, 2010 Share Posted September 17, 2010 Hello, Basically I am trying to create a SQL statement that joins data from 2 tables. I have tried many methods but come up short everytime. I need to select all from table1, and the last ID from table2 where table1.filed1 = table2.field2. So far I only get as far as SELECT field1, field2 AS messageFROM table1 LEFT OUTER JOIN table2ON table1.field1 = table2.field2ORDER BY table2.field2 DESC Which returns every result where table1.field1 = table2.field2 and I need it to only limit 1 per entry in table1. Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/ Share on other sites More sharing options...
ScopeXL Posted September 17, 2010 Author Share Posted September 17, 2010 I seemed to find a query that does part of what I want SELECT *FROM web_users LEFT OUTER JOIN web_messagesON web_users.username = web_messages.userGROUP BY web_users.username But I still need it to select the LAST id from table2 (web_messages). I tried using ORDER BY and it errored on me. Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1111990 Share on other sites More sharing options...
mikosiko Posted September 17, 2010 Share Posted September 17, 2010 try this SELECT a.*, (SELECT MAX(b.ID) FROM web_messages b WHERE b.ID = a.ID) AS Mid FROM web_users a Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1112108 Share on other sites More sharing options...
s0c0 Posted September 17, 2010 Share Posted September 17, 2010 The sub select should be avoided at all cost as they are very slow. Left Join should be avoided as well. Please post the schema of your table. Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1112132 Share on other sites More sharing options...
mikosiko Posted September 17, 2010 Share Posted September 17, 2010 that is a very wide brush to paint the water.... IMHO Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1112147 Share on other sites More sharing options...
s0c0 Posted September 17, 2010 Share Posted September 17, 2010 that is a very wide brush to paint the water.... IMHO IMHO its very true. Always explore other options if you find yourself writing sub queries they are obviously slower and the Big O proves it and try to avoid LEFT JOINS because it analyzes more than you may need, but hey I'm not looking to get into a debate. Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1112191 Share on other sites More sharing options...
ScopeXL Posted September 17, 2010 Author Share Posted September 17, 2010 CREATE TABLE IF NOT EXISTS `web_messages` ( `id` int(11) NOT NULL auto_increment, `user` text, `date` text, `name` text, `fromA` text, `toA` text, `channel` text, `text` text, `extra` text, `color` text, `time` int(11) NOT NULL default '0', `function` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=140877 ; CREATE TABLE IF NOT EXISTS `web_users` ( `id` int(11) NOT NULL auto_increment, `username` text, `password` text, `channel` text, `server` text, `version` text, `views` int(11) NOT NULL default '0', `requests` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=65 ; There is the schema for the 2 tables I am trying to join. Thanks for your help so far. Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1112196 Share on other sites More sharing options...
s0c0 Posted September 17, 2010 Share Posted September 17, 2010 mikosiko won't do the job because you are not storing the user id in the web_messages table. Just an FYI, in the messages table you should store users by the web_users.id rather than username. Searches on text take longer and you're having to store more data that way. Just sayin'. See if this works for you: SELECT web_users.*,web_messages.* FROMweb_users LEFT JOIN web_messages ON web_users.username = web_massages.userGROUP BY web_users.id ORDER BYweb_messages.time DESC else you might have to sub select: SELECT web_users.*, (SELECT web_messages.id FROM web_messages WHERE web_users.username = web_messages.user ORDER BY time DESC LIMIT 1) as web_massages_id FROMweb_users Quote Link to comment https://forums.phpfreaks.com/topic/213637-creating-a-join-statement/#findComment-1112227 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.