English 中文(简体)
MySQL—— 2. 选择不同的行文,以期
原标题:MySQL -- Getting distinct rows in a view
  • 时间:2010-05-20 05:50:23
  •  标签:
  • mysql
  • view

页: 1 我试图表明这一点:

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

但是,由于它有了一个nes问,我不得不把它当作一种观点。 目前,我认为这项工作的最佳问题是:

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)

我只希望每个职位一行,我希望是最新的。 是否有任何建议? 在处理诸如软性删除和从理论上说不及使我想要的数据更容易查询等事项时,用观点来尝试并使生活更加容易。

预言如此之多!

问题回答

 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

这样做是否有什么用处? 我不敢肯定这一点,因为它包含一个分局,它给你带来麻烦,但在不同的地方。

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

业绩可能不是写家事。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签