SELECT查询中的关系(PHP)

I have little problem with SQL query in PHP. I want to get from table post rows with category ex. 'art'. This table don't contain name of my category (only category_id). So, how to connect this in my query?

Tables:

post:

id, title, category_id

category:

id, name

I tried this way, but it not works.

SELECT * FROM post WHERE category_id = category.id AND category.name="art";

Anyone can help me? Thanks.

You are missing the JOIN

SELECT * FROM post 
INNER JOIN category 
WHERE category_id = category.id AND category.name="art";

You need to use join to query on name column of category table, e.g.:

SELECT p.*
FROM post p JOIN category c ON p.category_id = c.id
WHERE c.name = 'art';

You need to join the tables.

Given your schema, this should give you an starting point:

SELECT p.* FROM post p INNER JOIN category c ON p.category_id = c.id WHERE c.name = 'art';