2010-05-20 33 views
1

好的,在這裏。我試圖讓一個觀點,即表明這一點:MySQL - 在視圖中獲取不同的行

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

會是這樣的任何用途?我不知道這件事,因爲它包含一個子查詢,這是給你的麻煩,但在不同的地方......

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

性能可能不是什麼大書特書,無論是。

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