newbtophp Posted September 19, 2010 Share Posted September 19, 2010 I currently have the query below: SELECT table1.id, table1.review, table1.time, table2.author, table2.title FROM table1, table2 WHERE table1.id = table2.id AND table1.reviewer = '{$username}' ORDER BY table1.id Im using the above quite alot around my sites code...and sometimes adding the table prefixes etc. before the column names can make the query very long and take up quite alot of lines. Is their a way to make the above query more simple/easier, as its quite basic as I just followed the basic tizag examples. Hope someone can help! Quote Link to comment https://forums.phpfreaks.com/topic/213772-can-this-be-simplified/ Share on other sites More sharing options...
s0c0 Posted September 19, 2010 Share Posted September 19, 2010 Thats a pretty small query. I think you're fine. One practice I've taken to making my queries easier on the eyes is formatting them like so: SELECT table1.id, table1.review, table1.time, table2.author, table2.title FROM table1, table2 WHERE table1.id = table2.id AND table1.reviewer = '{$username}' ORDER BY table1.id Quote Link to comment https://forums.phpfreaks.com/topic/213772-can-this-be-simplified/#findComment-1112690 Share on other sites More sharing options...
kickstart Posted September 19, 2010 Share Posted September 19, 2010 Hi You can use aliases for the tables:- SELECT a.id, a.review, a.time, b.author, b.title FROM table1 a, table2 b WHERE a.id = b.id AND a.reviewer = '{$username}' ORDER BY a.id I also think it is more readable to move the JOIN clauses into an ON clause rather than leaving them in the WHERE clause. SELECT a.id, a.review, a.time, b.author, b.title FROM table1 a INNER JOIN table2 b ON a.id = b.id WHERE a.reviewer = '{$username}' ORDER BY a.id All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213772-can-this-be-simplified/#findComment-1112974 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.