database - MySQL 查询 : list of tag. 名称和语言尚未组合插入 images_urls 中

标签 database mysql

我想不出任何好的方法来获取尚未组合插入 images_urls 中的 tag.names 和 lang 列表。

我的数据库看起来像这样。

tags

  name    user_id
+-------+---------+
|hi     |    1    | 
|friend |    1    |
|friend |    2    |
|people |    2    |
+-------+---------+

users

 id   lang
+---+------+
| 1 |  en  |
| 2 |  sv  |
+---+------+

images_urls

  name     lang
+--------+------+
| hi     |  en  |
| friend |  sv  |
+--------+------+

What I would like to have returned would be:

result

 name     lang
+-------+------+
|friend |  en  |
|people |  sv  |
+-------+------+

I have tried something like this:

SELECT tags.name, users.lang
FROM tags, users
WHERE tags.user_id = users.id
AND CONCAT(tags.name, ',', users.lang) NOT IN(
    SELECT DISTINCT CONCAT(name, ',', lang) FROM images_urls
)
GROUP BY CONCAT(name, ',', lang)
ORDER BY SUM(tags.count) DESC
LIMIT 20;

最佳答案

我不确定为什么 images_urls 有一个名称而不是 user_id,但这应该有效:

SELECT t.name, u.lang
FROM tags t
JOIN users u ON ( u.id = t.user_id )
LEFT JOIN images_urls iu ON ( iu.name=t.name AND iu.lang=u.lang )
WHERE iu.name IS NULL

使用 LEFT JOIN 对于 images_urls 中不存在的行返回 NULL,因此我会进行检查。

关于database - MySQL 查询 : list of tag. 名称和语言尚未组合插入 images_urls 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2319822/

相关文章:

c# - 使用c#备份数据库,bak文件越来越大,如何阻止它增长?

mysql - Sql:使用表的列值作为存储过程的参数

MYSQL表搜索-过滤

python - 对多索引数据库文件求和

php - 数据库效率/结构问题

java - PreparedStatement 中的这些值/参数是什么意思?

mysql - 如何围绕表循环进行内部连接

php - 如何将传递的字符串匹配到检查 mysql 数据库的 php 函数

python - 为什么游标没有再次创建?

MYSQL 全文匹配不返回预期结果