好的,这里有。我想创建一个显示这个的视图:
+---------+---------+-------+--------+------------+---------------------+---------------+-------------------+-------------+
| 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)
执行此操作的查询是:
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
但是,由于它有嵌套查询,我不能将它用作视图。目前,我能想到的最好的查询是一个视图:
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
但那只是回归:
+---------+---------+-------+-----------+----------------+---------------------+---------------+-------------------+-------------+
| 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)
我每个post_id只需要一行,我希望它是最新的一行。有没有人有什么建议?在处理软删除和诸如此类的事情时,我正在使用视图来尝试让生活变得更轻松,理论上,从长远来看,我还可以更轻松地查询我想要的数据。
提前非常感谢!
答案 0 :(得分:1)
您可以使用
GROUP BY post_data.post_id HAVING MAX(post_data.time_stamp)
只获得最新的一行。所以完整的查询可能如下所示:
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
答案 1 :(得分:0)
这样的东西会有用吗?我不确定它,因为它包含一个子查询,它给你带来麻烦,但是在另一个地方......
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);
表演可能也不值得回家。