1

我有这个查询,它正确显示结果。我没有使用 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 
  1. 这样做是否足够:connections.word_id = words_en.id加入?

  2. 我还想为此添加子查询。那是什么语法?

子查询应该计算conenctions.word_id出现在连接表中的次数,其中connections.user_id != 1ergo 其他用户使用这个词的次数。

  1. 我还需要另一个可以让我users.name_surname获得words_en.user_id.
4

2 回答 2

0

要连接表,我们可以使用 ,(逗号)或 join 关键字。如果我们使用 ,(逗号),它将根据我们的条件给出准确的结果。如果我们使用 join 关键字,我们可以使用多种类型的连接,例如 left join right 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是子查询

对于您的第三点,请尝试以下

SELECT users.name_surname 
FROM users, words_en
WHERE users.user_id = words_en.user_id
ORDER BY users.user_id
于 2013-11-01T11:03:53.597 回答
0

对于 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
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
于 2013-11-01T11:13:32.073 回答