简体   繁体   English

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

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

This query borders on the most complex one I've ever attempted. 此查询与我尝试过的最复杂的查询相邻。

My tables: (I removed irrelevant 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    |                |
+--------------+--------------+------+-----+---------+----------------+

Relations: 关系:

Posts.id > Containers.pid Posts.id> Containers.pid

Containers.iid > Images.id Containers.iid> Images.id

I want to: 我想要:

Get all Posts where the corresponding Images.Category is one of value1, value2 or value3. 获取所有Posts,其中相应的Images.Category是value1,value2或value3之一。 Order Posts by Posts.Time. 按Posts.Time订购帖子。

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

Attempts: 尝试:

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'); 

Result: Ok, but not getting the Posts sorted by Posts.Time. 结果:好的,但是没有按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;

Result: Getting lines for Posts which does not contain Images.Category. 结果:获取不包含Images.Category的帖子行。

I hope my question is properly formatted, I appreciate your time. 希望我的提问格式正确,感谢您的宝贵时间。

try this: 尝试这个:

 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