简体   繁体   English

MySQL - 来自两列的不同值

[英]MySQL - distinct values from two columns

I have a table with multiple columns from which I need to get unique values from two of them. 我有一个包含多列的表,我需要从中获取其中两列的唯一值。

The table looks something like this 该表看起来像这样

msgid    sender    receiver  payload
1        service1  service2  xxx
2        service1  service3  xxx
3        service2  service3  xxx
...

The desired output should be: service1, service2, service3 (distinct values from columns sender and receiver) 所需的输出应该是: service1,service2,service3 (来自发送方和接收方列的不同值)

I found many different approaches on how to do this and chose using UNION like this: 我找到了很多不同的方法来解决这个问题,并选择使用UNION:

String query = "(SELECT sender AS service FROM messages) UNION " +
        "(SELECT receiver AS service FROM messages) ORDER BY service";

I tried also 我也试过了

String query = "(SELECT DISTINCT sender AS service FROM messages) UNION " +
        "(SELECT DISTINCT receiver AS service FROM messages) ORDER BY service";

which should produce the same since UNION already returns unique values. 由于UNION已经返回唯一值,因此应该生成相同的值。 However, I am getting mysql exception: 但是,我得到mysql异常:

java.sql.SQLException: Column 'sender' not found.

But column sender exists! 但是列发送者存在! When I run 我跑的时候

String query = "SELECT DISTINCT sender FROM messages";

everything works just fine. 一切正常。 Could you tell me, please, what am I doing wrong? 你能告诉我,我做错了什么? I tried to use similar statement as this post suggested . 我试图使用这篇文章建议的类似声明。 Thanks! 谢谢!

EDIT: this is the EXACT query I am using right now: 编辑:这是我现在使用的EXACT查询:

String query = "SELECT sender AS service FROM messages UNION SELECT receiver AS service FROM messages ORDER BY service";

This query should work as mentioned in the comments. 此查询应该如注释中所述那样工作。 However, I still get the exception. 但是,我仍然得到例外。 Why so? 为什么这样? Thanks for troubleshooting... 谢谢你排除故障......

My mysql version is 5.6. 我的mysql版本是5.6。

EDIT2: I had no idea that the problem could be with JDBC and not my statement. 编辑2:我不知道问题可能是JDBC,而不是我的陈述。 I apologiye for not providing this information in my question... 我抱歉没有在我的问题中提供这些信息......

A guess in the dark is when you retrieve the data you use "sender" and not "service" that could explain why the first 2 queries work and the last doesn't. 黑暗中的猜测是当你检索数据时,你使用“发送者”而不是“服务”,这可以解释为什么前2个查询有效,最后一个查询没有。

Let me know if this is the case. 如果是这种情况,请告诉我。

Assuming you are using JDBC : 假设您正在使用JDBC

    String query = "SELECT sender AS service FROM messages UNION SELECT receiver AS service FROM messages ORDER BY service";
    ResultSet rs = ps.executeQuery(query);
    while (rs.next()) {
        String value = rs.getString("sender"); // instead it should be "service"
        // Some other actions
    }

It should be this simple because UNION already removes duplicates 应该这么简单,因为UNION已经删除了重复项

SELECT sender AS service FROM messages
UNION
SELECT receiver AS service FROM messages
ORDER BY service;

The MySQL docs for UNION agree with this and my sort here UNIONMySQL文档同意这个以及我的排序

Instead, provide a column alias in the first SELECT statement and refer to the alias in the ORDER BY 而是在第一个SELECT语句中提供列别名,并引用ORDER BY中的别名

On SQLFiddle my query works http://sqlfiddle.com/#!2/c64e4/7 and does the OPs http://sqlfiddle.com/#!2/c64e4/8 在SQLFiddle上我的查询工作http://sqlfiddle.com/#!2/c64e4/7并执行操作系统http://sqlfiddle.com/#!2/c64e4/8

只需从你的代码中删除"+"符号和"Distinct" ,然后尝试..

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

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