Yervand Khalapyan
Yervand Khalapyan

Reputation: 858

Sql query selecting from both tables.

I have a query which looks like this
$db->query("SELECT A.page_id, A.page_name FROM user_likes_pages as A , user_likes as B WHERE A.page_id = B.page_id AND B.country_id = ".$user_reg." ");
The thing is, I want to select a column from user_likes. Do I have to make a join or I can do it in different way. Thank you.

Upvotes: 0

Views: 35

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269483

You have a join in your query, but it is implicit. You should write the query as:

SELECT ulp.page_id, ulp.page_name, ul.<whatever>
FROM user_likes ul JOIN
     user_likes_pages ulp
     ON ul.page_id = ulp.page_id 
WHERE ul.country_id = ".$user_reg."

In addition to adding the explicit join syntax, I also changed the table aliases so they are abbreviations of the table name. This makes it easier to read the query and avoid mistakes.

Upvotes: 1

Patricia
Patricia

Reputation: 2865

You select B.columnname. You don't need a join, because you have the A.page_id = B.page_id.

Upvotes: 1

Related Questions