-2

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

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

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

然后我想加入他们,这样它就会从 post 表中的 post_category 中选择所有 postId

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    |                |
+------------+-------------+------+-----+---------+----------------+
4

3 回答 3

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用于不同的帖子。

于 2013-09-07T23:42:07.627 回答
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 语句

于 2013-09-07T23:40:20.363 回答
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
于 2013-09-07T23:43:34.210 回答