我有这个查询,它正确显示结果。我没有使用join,但它似乎仍然返回正确的值,这怎么可能?
SELECT connections.id,
connections.word_id,
connections.order_id,
connections.top,
connections.deleted,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(connections.modified)) AS modified_ago,
words_en.word,
(CASE WHEN words_en.user_id = 1 THEN "You" ELSE words_en.user_id END) AS created_by
FROM connections, words_en
WHERE connections.word_id = words_en.id AND connections.user_id = 1
IN (SELECT COUNT(*) connections WHERE /* Result of previous select connections.word_id */ AND connections.user_id != 1)
ORDER BY connections.order_id
这样做是否足够:connections.word_id = words_en.id
加入?
我还想为此添加子查询。这是什么语法?
子查询应计算conenctions.word_id
在连接表中显示的次数connections.user_id != 1
ergo其他用户使用此单词的次数。
users.name_surname
的{{1}}。答案 0 :(得分:0)
要连接我们可以使用的表,(逗号)或连接关键字。 如果我们使用,(逗号)它将根据我们的条件给出确切的结果。 如果我们使用join关键字,我们可以使用许多类型的连接,如左连接右连接等等。这些连接将产生不同的结果。
我们可以说,(逗号)连接与JOIN相同但不是LEFT JOIN。
以下是编写子查询的示例
SELECT connections.id,
connections.word_id,
connections.order_id,
connections.top,
connections.deleted,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(connections.modified)) AS modified_ago,
words_en.word,
words_en.user_id
FROM connections, words_en
WHERE connections.word_id = words_en.id AND connections.user_id IN (select id from connections)
ORDER BY connections.order_id
select id from connections
是子查询
第3点尝试以下
SELECT users.name_surname
FROM users, words_en
WHERE users.user_id = words_en.user_id
ORDER BY users.user_id
答案 1 :(得分:0)
用于连接操作:
SELECT connections.id,
connections.word_id,
connections.order_id,
connections.top,
connections.deleted,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(connections.modified)) AS modified_ago,
words_en.word,
words_en.user_id
FROM connections
LEFT JOIN words_en ON connections.word_id = words_en.id AND connections.user_id = 1
WHERE
words_en.id IS NOT NULL
ORDER BY connections.order_id