pv1
pv1

Reputation: 317

Is this join correct?

I am very new to MySQL joins and I have put this together, it is working but I don't know if there is a better way to do this??

$q = $dbc -> prepare("SELECT l.layout, a.email 
                      FROM layout AS l 
                      JOIN accounts AS a ON (l.id = a.id) 
                      WHERE email = ?");
$q -> execute(array($_POST['email']));

Basically there are 2 tables one called layout and one called accounts, I want to select the layout from layout where the email that matches the id in accounts?

Upvotes: 1

Views: 49

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270775

There doesn't appear to be a better way. Yours is fine. The only suggestion is not to use the AS keyword for table aliases. The AS in that position is valid in MySQL, but just isn't commonly used. It may not be valid in every RDBMS so it may be best to not to get in the habit of using.

SELECT l.layout, a.email 
FROM layout l JOIN accounts a ON (l.id = a.id) 
--          ^               ^
-- alias follows table name directly
WHERE email = ?

Upvotes: 1

Related Questions