Jump to content

Can this be simplified?


newbtophp

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/213772-can-this-be-simplified/
Share on other sites

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.