[英]MySQL — Getting distinct rows in a view
OK, here goes. 好,去 I am trying to make a view that shows this:
我正在尝试显示一个视图:
+---------+---------+-------+--------+------------+---------------------+---------------+-------------------+-------------+
| post_id | status | title | body | ip_address | time_stamp | category_name | sub_category_name | post_type |
+---------+---------+-------+--------+------------+---------------------+---------------+-------------------+-------------+
| 1 | enabled | test | test 2 | | 2010-05-20 01:22:17 | For Sale | Computers | transaction |
+---------+---------+-------+--------+------------+---------------------+---------------+-------------------+-------------+
1 row in set (0.00 sec)
The query that did this is: 执行此操作的查询是:
SELECT post.id AS post_id,
post.status AS status,
post_data.title AS title,
post_data.body AS body,
post_data.ip_address AS ip_address,
post_data.time_stamp AS time_stamp,
post_category.name AS category_name,
post_sub_category.name AS sub_category_name,
post_category.type AS post_type
FROM post,
(
SELECT * FROM post_data WHERE post_data.post_id = post_id ORDER BY post_data.post_id DESC LIMIT 1
) AS post_data,
post_sub_category,
post_category
WHERE
post.sub_category_id = post_sub_category.id AND
post_sub_category.category_id = post_category.id
But, since it has a nested query, I can't use it as a view. 但是,由于它具有嵌套查询,因此无法将其用作视图。 Currently the best query I can think of that works as a view is this:
目前,我能想到的最好的查询视图是:
SELECT
post.id AS post_id,
post.status AS status,
post_data.title AS title,
post_data.body AS body,
post_data.ip_address AS ip_address,
post_data.time_stamp AS time_stamp,
post_category.name AS category_name,
post_sub_category.name AS sub_category_name,
post_category.type AS post_type
FROM post,
post_data,
post_sub_category,
post_category
WHERE
post.sub_category_id = post_sub_category.id AND
post_sub_category.category_id = post_category.id
ORDER BY post_data.id DESC
But that just returns: 但这只是返回:
+---------+---------+-------+-----------+----------------+---------------------+---------------+-------------------+-------------+
| post_id | status | title | body | ip_address | time_stamp | category_name | sub_category_name | post_type |
+---------+---------+-------+-----------+----------------+---------------------+---------------+-------------------+-------------+
| 1 | enabled | test | test 2 | | 2010-05-20 01:22:17 | For Sale | Computers | transaction |
| 1 | enabled | TEST | TEST BODY | 192.168.10.155 | 2010-05-19 23:09:15 | For Sale | Computers | transaction |
+---------+---------+-------+-----------+----------------+---------------------+---------------+-------------------+-------------+
2 rows in set (0.00 sec)
I only want one row per post_id, and I want it to be the newest one. 我只希望每个post_id有一行,并且我希望它是最新的。 Does anyone have any suggestions?
有没有人有什么建议? I'm using views to try and make life easier when it comes to dealing with things such as soft deletes and whatnot and also to, theoretically, make querying for the data I want easier in the long-run.
在处理软删除和诸如此类的事情时,我使用视图来尝试使生活变得更轻松,并且从长远来看,从理论上讲,这也使查询我想要的数据变得更加轻松。
Thanks so much in advance! 非常感谢!
You may use 您可以使用
GROUP BY post_data.post_id HAVING MAX(post_data.time_stamp)
to get only the newest row. 仅获取最新行。 So the complete query might look like this:
因此,完整的查询可能如下所示:
SELECT
post.id AS post_id,
post.status AS status,
post_data.title AS title,
post_data.body AS body,
post_data.ip_address AS ip_address,
post_data.time_stamp AS time_stamp,
post_category.name AS category_name,
post_sub_category.name AS sub_category_name,
post_category.type AS post_type
FROM post,
post_data,
post_sub_category,
post_category
WHERE
post.sub_category_id = post_sub_category.id AND
post_sub_category.category_id = post_category.id
GROUP BY post_data.post_id HAVING MAX(post_data.time_stamp)
ORDER BY post_data.id DESC
Would something like this be of any use? 这样的东西有用吗? I'm not sure about it, as it contains a subquery, which was giving you trouble, but in a different place...
我不确定,因为它包含一个子查询,这给您带来麻烦,但是在另一个地方...
SELECT post.id AS post_id,
post.status AS status,
post_data.title AS title,
post_data.body AS body,
post_data.ip_address AS ip_address,
post_data.time_stamp AS time_stamp,
post_category.name AS category_name,
post_sub_category.name AS sub_category_name,
post_category.type AS post_type
FROM post,
post_data,
post_sub_category,
post_category
WHERE post.sub_category_id = post_sub_category.id AND
post_sub_category.category_id = post_category.id AND
post_data.post_id = post_id AND
post_data.time_stamp = (SELECT MAX(time_stamp)
FROM post_data
WHERE post_data.post_id = post_id);
The performance might not be anything to write home about, either. 性能可能也不是值得一提的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.