繁体   English   中英

MySQL查询,无法弄清楚如何获取正确的数据

[英]MySQL query, can't figure out how to fetch the correct data

此查询与我尝试过的最复杂的查询相邻。

我的表格:(我删除了不相关的数据)

mysql> describe Posts;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| Time      | int(11) | YES  | MUL | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

mysql> describe Containers;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| pid      | int(11)     | YES  | MUL | NULL    |                |
| iid      | int(11)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

mysql> describe Images;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| LocalPath    | varchar(100) | YES  |     | NULL    |                |
| Category     | varchar(20)  | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

关系:

Posts.id> Containers.pid

Containers.iid> Images.id

我想要:

获取所有Posts,其中相应的Images.Category是value1,value2或value3之一。 按Posts.Time订购帖子。

伪代码:在WHERE Images.Category IN('value1','value2,'value3')ORDER BY Posts.Time DESC中获取所有数据。

尝试:

SELECT * FROM Images I
INNER JOIN Containers C ON C.iid = I.id
INNER JOIN Posts P ON C.pid = P.id
WHERE I.Category IN('value1','value2','value3'); 

结果:好的,但是没有按Posts.Time对Posts进行排序。

SELECT * FROM Posts P
INNER JOIN Containers C ON P.id = C.pid
RIGHT OUTER JOIN Images I ON C.iid = I.id
WHERE I.Category IN('value1','value2','value3')
ORDER BY P.Time DESC;

结果:获取不包含Images.Category的帖子行。

希望我的提问格式正确,感谢您的宝贵时间。

尝试这个:

 SELECT * FROM Images I
 INNER JOIN Containers C ON C.iid = I.id
 INNER JOIN Posts P ON C.pid = P.id
 WHERE I.Category IN('value1','value2','value3')
 ORDER BY P.Time DESC;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM