简体   繁体   English

如何选择mysql结果?

[英]How to select mysql results?

invoice 发票

+----+-----+---------+-------+
| Sr | BRN |  Name   |  Amnt |
+----+-----+---------+-------+
| 1  | 1   | John    |  10   |
| 2  | 1   | John    |   4   |
| 3  | 2   | Belly   |   4   |
| 4  | 3   | John    |  14   |
| 5  | 4   | John    |   5   |
| 6  | 4   | John    |  14   |
+----+-----+---------+-------+

I want to select all rows except the duplicate BRN. 我想选择除重复的BRN之外的所有行。 (If there are two/more ge in BRN then it should only select one) (如果BRN中有两个/多个ge,则只能选择一个)

I tried: 我试过了:

SELECT *(DISTINCT BRN) FROM invoice

Expected result: 预期结果:

+-----+---------+-------+
| BRN |  Name   |  Amnt |
+-----+---------+-------+
| 1   | John    |  10   |
| 2   | Belly   |   4   |
| 3   | John    |  14   |
| 4   | John    |   5   |
+-----+---------+-------+

选择*从发票日期> =:fdate GROUP BY BRN

Given the following table: 给出下表:

+----+-----+---------+-------+
| Sr | BRN |  Name   |  Amnt |
+----+-----+---------+-------+
| 1  | 1   | John    |  10   |
| 2  | 1   | John    |   4   |
| 3  | 2   | Belly   |   4   |
| 4  | 3   | John    |  14   |
| 5  | 4   | John    |   5   |
| 6  | 4   | John    |  14   |
+----+-----+---------+-------+

with the expected results: 具有预期的结果:

+-----+---------+-------+
| BRN |  Name   |  Amnt |
+-----+---------+-------+
| 1   | John    |  10   |
| 2   | Belly   |   4   |
| 3   | John    |  14   |
| 4   | John    |   5   |
+-----+---------+-------+

The difficult part is getting the amount, because it is arbitrary, not to mention that the values in Amnt are pretty much worthless in this result. 困难的部分是获取金额,因为它是任意的,更不用说Amnt中的值在此结果中几乎毫无价值。

If you want distinct BRN, the query would be SELECT DISTINCT BRN FROM invoice 如果要使用不同的BRN,查询将是SELECT DISTINCT BRN FROM invoice

You might even get away with SELECT DISTINCT BRN, Name FROM invoice 您甚至可以SELECT DISTINCT BRN, Name FROM invoice

An intermediate step would be SELECT BRN,Name FROM invoice GROUP BY BRN, Name 中间步骤是SELECT BRN,Name FROM invoice GROUP BY BRN, Name

But if you try to include Amnt in the equation, then the query will fail because there's no way for the database to determine which Amnt to show. 但是,如果您尝试在公式中包含Amnt ,则查询将失败,因为数据库无法确定要显示的Amnt。

So, you could try this kludge: 因此,您可以尝试以下操作:

SELECT a.BRN, a.Name, b.Amnt FROM invoice AS a LEFT JOIN invoice AS b ON a.BRN=b.BRN

No guarantees on which Amnt it will pick up, though. 但是,不能保证会选择哪个Amnt。

Hope that helps. 希望能有所帮助。

在此处输入图片说明

在此处输入图片说明

See Here Use GROUP BY in Query with your Conditions 请参见此处在查询中使用GROUP BY查询您的条件

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

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