开发者

How to get data from 2 tables? [duplicate]

This question already has answers here: Querying 2 Tables in a single query (3 answers) Closed 9 years ago.

I am trying to create a website with an existing database. The database has two tables, one of them contain the post info like title, id, content, etc etc, the other table contains the post ID and the post category.

For example: I want to take a post from a specific category.

$sql = 'SELECT * FROM posts WHERE status = "publish" ORDER BY date DESC LIM开发者_如何转开发IT 10';

This is a simple function, can some one help me please how to modify it, to get the category from the other table and the post info from the other table.


SELECT * FROM posts, posts_category WHERE category = 'category' 
AND 
status = 'publish'
AND
posts_category.post_id = posts.id
ORDER BY date DESC LIMIT 10;


  $sql = "SELECT * FROM posts WHERE status = 'publish' AND pid IN (SELECT pid FROM pcategory WHERE ptype='category_name') ORDER BY date DESC LIMIT 10";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜