从MySQL数据库中选择两行,作为依赖属性的一行。

问题描述 投票:1回答:1

我有一个FreeRADIUS,它有一个MySQL数据库后端。用户存储在radcheck表中。

radcheck table image

我想从中选择所有的用户(在不久的将来会有更多的用户),包括用户名,速度曲线和密码。所以我将得到的结果是lukasfazik,testovacieheslo,OPTIK100。

SELECT  username, password, profile
FROM (SELECT t1.username, t1.value AS password, t2.value AS profile
      FROM radcheck AS t1, radcheck AS t2
      WHERE t1.value != t2.value
     ) AS arrgh;

我试过这个:

result from my query image

GROUP BY不起作用,我得到了一个错误。

[42000][1055] 表达式#2的SELECT列表不在GROUP BY子句中,并且包含非聚合列'arrgh.password',它在功能上不依赖于GROUP BY子句中的列;这与sql_mode=only_full_group_by不兼容。

python mysql sql freeradius
1个回答
0
投票

看起来你想对你的表进行透视。在这种情况下,最简单的方法是将表连接到自身。对于更复杂的情况,你可以看看这个 指南:

select a.*, b.password 
from (username, value profile from FreeRADIUS where attribute = 'user-profile') a
join (username, value password from FreeRADIUS where attribute = 'cleartext-password') b 
on a.username = b.username

0
投票

你可以使用子查询选择你想要的属性,并将它们连接起来。

> SELECT * FROM
    (SELECT username, value as profile FROM radcheck WHERE attribute = "User-Profile") as t1
NATURAL JOIN
    (SELECT username, value as pass FROM radcheck WHERE attribute = "Cleartext-Password") as t2
;

+------------+----------+-----------------+
| username   | profile  | pass            |
+------------+----------+-----------------+
| lukasfazik | OPTIK100 | testovacieheslo |
+------------+----------+-----------------+

子查询将包含主表的用户名和所选属性的值。

> SELECT username, value as profile FROM radcheck WHERE attribute = "User-Profile"

+------------+-----------+
| _username_ | _profile_ |
+------------+-----------+
| lukasfazik | OPTIK100  |
+------------+-----------+
| testuser   | OPTIK200  |
+------------+-----------+
| fooUser    | OPTIK500  |
+------------+-----------+
| ...        | ...       |
+------------+-----------+

通过嵌套自然连接,可以以类似的方式添加更多属性。

© www.soinside.com 2019 - 2024. All rights reserved.