简体   繁体   English

MySQL - 选择所有列,其中一列是 DISTINCT

[英]MySQL - SELECT all columns WHERE one column is DISTINCT

I'm very sorry if the question seems too basic.如果这个问题看起来太基础了,我很抱歉。
I've surfed entire Internet and StackOverflow for a finished solution, and did not find anything that I can understand, and can't write it myself, so have to ask it here.我翻遍了整个互联网和 StackOverflow 寻找一个完整的解决方案,但没有找到任何我能理解的东西,而且我自己也不会写,所以只好在这里问了。

I have a MySQL database.我有一个 MySQL 数据库。
It has a table named "posted".它有一个名为“posted”的表。
It has 8 columns.它有 8 列。

I need to output this result:我需要输出这个结果:

SELECT DISTINCT link FROM posted WHERE ad='$key' ORDER BY day, month

But I need not only the "link" column, but also other columns for this row.但我不仅需要“链接”列,还需要该行的其他列。
Like for every row returned with this query I also need to know its "id" in the table, "day" and "month" values etc.与此查询返回的每一行一样,我还需要知道其在表中的“id”、“日”和“月”值等。

Please tell me what should I read to make it, or how to make it.请告诉我我应该阅读什么来制作它,或者如何制作它。
Please keep it as simple as possible, as I'm not an expert in MySQL.请尽可能简单,因为我不是 MySQL 专家。

Edit: I tried this:编辑:我试过这个:

SELECT DISTINCT link,id,day,month FROM posted WHERE ad='$key' ORDER BY day, month

It doesn't work.它不起作用。 It returns too many rows.它返回太多行。 Say there are 10 rows with same links, but different day/month/id.假设有 10 行具有相同的链接,但日期/月份/ID 不同。 This script will return all 10, and I want only the first one (for this link).此脚本将返回所有 10 个,而我只想要第一个(对于此链接)。

The problem comes from instinctively believing that DISTINCT is a local pre-modifier for a column.问题来自本能地认为DISTINCT是列的局部预修饰符。

Hence, you "should" be able to type因此,您“应该”能够输入

XXbadXX SELECT col1, DISTINCT col2 FROM mytable XXbadXX

and have it return unique values for col 2. Sadly, no.并让它返回第 2 col的唯一值。遗憾的是,没有。 DISTINCT is actually a global post-modifier for SELECT , that is, as opposed to SELECT ALL (returning all answers) it is SELECT DISTINCT (returning all unique answers). DISTINCT实际上是SELECT的全局后置修饰符,也就是说,与SELECT ALL (返回所有答案)相反,它是SELECT DISTINCT (返回所有唯一答案)。 So a single DISTINCT acts on ALL the columns that you give it.因此,单个DISTINCT作用于您提供的所有列。

This makes it real hard to use DISTINCT on a single column, while getting the other columns, without doing major extremely ugly backflips.这使得在不进行严重的极其丑陋的后空翻的情况下,很难在单个列上使用DISTINCT ,同时获取其他列。

The correct answer is to use a GROUP BY on the columns that you want to have unique answers: SELECT col1, col2 FROM mytable GROUP BY col2 will give you arbitrary unique col2 rows, with their col1 data as well.正确的答案是在您希望获得唯一答案的列上使用GROUP BYSELECT col1, col2 FROM mytable GROUP BY col2将为您提供任意唯一的col2行,以及它们的col1数据。

I tried this:我试过这个:

 SELECT DISTINCT link,id,day,month FROM posted WHERE ad='$key' ORDER BY day, month

It doesn't work.它不起作用。 It returns too many rows.它返回太多行。 Say there are 10 rows with same links, but different day/month/id.假设有 10 行具有相同的链接,但日期/月份/ID 不同。 This script will return all 10, and I want only the first one (for this link).此脚本将返回所有 10 个,而我只想要第一个(对于此链接)。

What you're asking doesn't make sense.你问的没有意义。

Either you want the distinct value of all of link, id, day, month , or you need to find a criterion to choose which of the values of id, day, month you want to use, if you just want at most one distinct value of link .您要么想要所有link, id, day, month的不同值,要么您需要找到一个标准来选择要使用的id, day, month值中的哪一个,如果您最多只想要一个不同的值的link

Otherwise, what you're after is similar to MySQL's hidden columns in GROUP BY / HAVING statements , which is non-standard SQL, and can actually be quite confusing.否则,您所追求的类似于MySQL 在GROUP BY / HAVING statements 中的隐藏列,这是非标准 SQL,实际上可能会非常混乱。

You could in fact use a GROUP BY link if it made sense to pick any row for a given link value.如果为给定的link值选择任何行是有意义的,您实际上可以使用GROUP BY link

Alternatively, you could use a sub-select to pick the row with the minimal id for a each link value (as described in this answer ):或者,您可以使用子选择来为每个link值选择具有最小id的行(如本答案中所述):

 SELECT link, id, day, month FROM posted
     WHERE (link, id) IN
           (SELECT link, MIN(id) FROM posted ad='$key' GROUP BY link)
SELECT Id, Link, Day, Month FROM Posted
WHERE Id IN(
   SELECT Min(Id) FROM Posted GROUP BY Link)
  SELECT OTHER_COLUMNS FROM posted WHERE link in (
  SELECT DISTINCT link FROM posted WHERE ad='$key' )
  ORDER BY day, month

If what your asking is to only show rows that have 1 link for them then you can use the following:如果您的要求是只显示具有 1 个链接的行,那么您可以使用以下内容:

SELECT * FROM posted WHERE link NOT IN 
(SELECT link FROM posted GROUP BY link HAVING COUNT(LINK) > 1)

Again this is assuming that you want to cut out anything that has a duplicate link.同样,这是假设您要删除任何具有重复链接的内容。

I think the best solution would be to do a subquery and then join that to the table.我认为最好的解决方案是进行子查询,然后将其连接到表中。 The sub query would return the primary key of the table.子查询将返回表的主键。 Here is an example:这是一个例子:

select *
from (
          SELECT row_number() over(partition by link order by day, month) row_id
          , *

          FROM posted 
          WHERE ad='$key' 
          ) x
where x.row_id = 1

What this does is the row_number function puts a numerical sequence partitioned by each distinct link that results in the query.它所做的是 row_number 函数放置一个数字序列,该序列由导致查询的每个不同链接分区。

By taking only those row_numbers that = 1, then you only return 1 row for each link.通过仅采用那些 = 1 的 row_numbers,那么您只为每个链接返回 1 行。

The way you change what link gets marked "1" is through the order-by clause in the row_number function.更改标记为“1”的链接的方式是通过 row_number 函数中的 order-by 子句。

Hope this helps.希望这可以帮助。

SELECT DISTINCT link,id,day,month FROM posted WHERE ad='$key' ORDER BY day, month

OR要么

SELECT link,id,day,month FROM posted WHERE ad='$key' ORDER BY day, month

If you want all columns where link is unique:如果您想要链接唯一的所有列:

SELECT * FROM posted WHERE link in
     (SELECT link FROM posted WHERE ad='$key' GROUP BY link);

What you want is the following:你想要的是以下内容:

SELECT DISTINCT * FROM posted WHERE ad='$key' GROUP BY link ORDER BY day, month

if there are 4 rows for example where link is the same, it will pick only one (I asume the first one).例如,如果有 4 行链接相同,它将只选择一个(我假设是第一个)。

I had a similar problem, maybe that help someone, for example - table with 3 columns我有一个类似的问题,也许这对某人有帮助,例如 - 有 3 列的表

SELECT * FROM DataTable WHERE Data_text = 'test' GROUP BY Data_Name ORDER BY Data_Name ASC

or要么

SELECT Data_Id, Data_Text, Data_Name FROM DataTable WHERE Data_text = 'test' GROUP BY Data_Name ORDER BY Data_Name ASC

Two ways work for me.有两种方法适合我。

In MySQL you can simply use "group by".在 MySQL 中,您可以简单地使用“group by”。 Below will select ALL, with a DISTINCT "col"下面将选择所有,带有不同的“col”

SELECT *
FROM tbl
GROUP BY col
SELECT a.* FROM orders a INNER JOIN (SELECT course,MAX(id) as id FROM orders WHERE admission_id=".$id." GROUP BY course ) AS b ON a.course = b.course AND a.id = b.id

With the Above Query you will get unique records with where condition使用上述查询,您将获得具有 where 条件的唯一记录

Select the datecolumn of month so that u can get only one row per link, eg:选择月份的日期列,以便每个链接只能获得一行,例如:

select link, min(datecolumn) from posted WHERE ad='$key' ORDER BY day, month

Good luck............祝你好运............

Or要么

u if you have date column as timestamp convert the format to date and perform distinct on link so that you can get distinct link values based on date instead datetime你如果你有日期列作为时间戳将格式转换为日期并在链接上执行不同,这样你就可以根据日期而不是日期时间获得不同的链接值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM