考虑以下查询和输出:
MariaDB [cf]> SELECT user_name, target_id
FROM table_google_apps
WHERE user_name LIKE "%stepha%" OR user_name LIKE "%tobias%"
LIMIT 5;
+----------------+----------------------------------------------+
| user_name | target_id |
+----------------+----------------------------------------------+
| Stephan B | 1S4H-FfVVkDG2kcJKyaZqnmLhVYSRIrZDlkRGQQ9P0Go |
| Stephan B | 1qzkKgYKXeZ671dmLswp-BFyF-cs4NtobZlt0KmE6mwg |
| Stephan B | 0B_qFxWHs9u2naGdRSlktY1dwR0E |
| Tobias S | 1qzkKgYKXeZ671dmLswp-BFyF-cs4NtobZlt0KmE6mwg |
| Stephan | 0B_qFxWHs9u2nOVdLQW4taXkycG8 |
+----------------+----------------------------------------------+
5 rows in set (0.00 sec)
可以清楚地看到Stephan和Tobias处理同一文档(target id 1qzkKgYKXeZ671dmLswp-BFyF-cs4NtobZlt0KmE6mwg
)。如何编写一个只返回stephan的target_id等于target_id形式的数据的查询?
例如:...... where stephan.target_id = tobias.target_id
..
谢谢!
答案 0 :(得分:0)
这将为您提供文件的所有用户:
SELECT
GROUP_CONCAT(user_name SEPARATOR ', ') AS users,
COUNT(*) AS user_count,
target_id
FROM
table_google_apps
GROUP BY
target_id
答案 1 :(得分:0)
您正在寻找自我加入:
SELECT DISTINCT t1.user_name, t1.target_id
FROM table_google_apps AS t1
INNER JOIN table_google_apps AS t2
ON t1.target_id = t2.target_id AND t1.user_name <> t2.user_name
WHERE t1.user_name LIKE "%stepha%" OR t1.user_name LIKE "%tobias%"
LIMIT 5;
查询根据相同的 target_id
值和不同的 user_name
值将表连接到自身。因此,它返回共享相同user_name
值的target_id
个值。
注意:只要您只对共享相同user_name
值的target_id
个值感兴趣,该查询就会有效。