加入来自不同表的两个查询

时间:2013-09-07 23:27:30

标签: php mysql sql join

如何将这两个查询合并为一个?

第一个查询从特定类别获取所有posts_id; post_category表中的postId等于post表中的id

select distinct postId 
  from post_category 
  where categoryId='125' or categoryId='3'

然后我想加入他们所以它将从post表中的post_category中选择所有po​​stId

SELECT * FROM post <<query one join>> AND approve=1"

  +--------------+-----------------------+------+-----+---------------------+----------------+
    | Field        | Type                  | Null | Key | Default             | Extra          |
    +--------------+-----------------------+------+-----+---------------------+----------------+
    | id           | int(11)               | NO   | PRI | NULL                | auto_increment |
    | autor        | varchar(40)           | NO   | MUL |                     |                |
    | date         | datetime              | NO   | MUL | 0000-00-00 00:00:00 |                |
    | short_story  | text                  | NO   | MUL | NULL                |                |
    | full_story   | text                  | NO   | MUL | NULL                |                |
    | xfields      | text                  | NO   |     | NULL                |                |
    | title        | varchar(255)          | NO   | MUL |                     |                |
    | descr        | varchar(200)          | NO   | MUL |                     |                |
    | keywords     | text                  | NO   |     | NULL                |                |
    | category     | varchar(200)          | NO   | MUL | 0                   |                |
    | alt_name     | varchar(200)          | NO   | MUL |                     |                |
    | comm_num     | mediumint(8) unsigned | NO   | MUL | 0                   |                |
    | allow_comm   | tinyint(1)            | NO   |     | 1                   |                |
    | allow_main   | tinyint(1) unsigned   | NO   | MUL | 1                   |                |
    | approve      | tinyint(1)            | NO   | MUL | 0                   |                |
    | fixed        | tinyint(1)            | NO   |     | 0                   |                |
    | allow_br     | tinyint(1)            | NO   |     | 1                   |                |
    | symbol       | varchar(3)            | NO   | MUL |                     |                |
    | tags         | varchar(255)          | NO   | MUL |                     |                |
    | metatitle    | varchar(255)          | NO   |     |                     |                |
    | FileTempUUID | varchar(11)           | YES  |     | NULL                |                |
    +--------------+-----------------------+------+-----+---------------------+----------------+

post_category;

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| cid        | bigint(11)  | NO   | PRI | NULL    | auto_increment |
| postId     | int(11)     | NO   |     | NULL    |                |
| categoryId | smallint(6) | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

3 个答案:

答案 0 :(得分:1)

作为您的表架构,请尝试此连接:

SELECT 
    p.* 
FROM 
    post p
LEFT OUTER JOIN post_category c ON (c.postId = p.id)
WHERE 
    c.categoryId IN (125,3) AND p.approve=1
GROUP BY p.id

如果需要,还可以使用GROUP BY p.id来填充不同的帖子。

答案 1 :(得分:0)

我认为变量{$ join}是指字符串这是完全错误的,在SQL中有一个叫做Join的特殊关键字,我会尽量写SQL语句来做我猜想你想要的

$sql = "Select a.postId, a.categoryId, b.* from post_category a, post b where a.postId = b.id and (a.categoryId='125' or a.categoryId='3')

@Edit 如果我理解你的评论你的代码会导致这个

 SELECT * FROM post select distinct postId 
  from post_category 
  where categoryId='125' or categoryId='3' AND approve=1"

哪个是错误的SQL语句

答案 2 :(得分:0)

这样的事情应该这样做:

SELECT DISTINCT postId post.* FROM post_category 
LEFT JOIN post ON post_category.postId = post.id
WHERE (categoryId='125' OR categoryId='3') AND post.approve = 1