SQL查询仅在满足特定条件时选择行

时间:2015-04-29 14:29:41

标签: mysql sql

考虑以下SQL查询:

SELECT
friends.name as friendname,
friends.surname as friendsurname,
friends.number as friendnumber,
friends.gender as friendgender, 
clients.name as clientname,
clients.surname as clientsurname
FROM friends
INNER JOIN clients
ON friends.clientid = clients.id
WHERE datetime(friends.creationdate, 'localtime') >= datetime('SOME_TEST_TIME') AND datetime(friends.creationdate, 'localtime') <= datetime('SOME_TEST_TIME')

friendsclients表格都有number列,我想在此查询中进行比较:IF friends.number已在clients&# 39;表格(clients.number列)然后不再选择该特定行。 如何在一个查询中完成此操作?

示例:

Table clients:
--------------------------
id  name  surname number

1   john  smith   55555
2   sam   wesker  12345
3   adam  Nye     48745
--------------------------

Table friends:

----------------------------------
id  name  surname number clientid

1   abcd  qwert   88888  2
2   dddd  asdfg   48745  2
3   ffff  zxcvb   77777  3
----------------------------------

查询应省略friends中的第二行,因为其编号出现在clients&#39;表

0 个答案:

没有答案