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
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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.