我有一个问题:
SELECT DISTINCT phone
FROM contacts
WHERE (phone='123456' OR phone='456789' OR phone='789789')
AND deleted=0 AND user=1
这将显示联系人表中的所有行,并且不会删除它们。如何返回联系人表中未找到的值?
注意: 我的表格中没有找到的字段。这就是我要归还的内容
例如:
| Phone | deleted | found |
|-----------|---------|-------|
| 123456 | 0 | YES |
| 456789 | 1 | YES |
| 789789 | 0 | NO | <---
谢谢, 尼科斯
答案 0 :(得分:1)
最简单的解决方案:检查PHP上的结果并验证不存在的手机。
修改强>
如果您想将其作为一个查询运行,您可以创建此语句:
SELECT t.phone
FROM
(
SELECT 123456 as phone
UNION ALL
SELECT 456789 as phone
UNION ALL
SELECT 789789 as phone
) as t
LEFT JOIN contacts as c
ON t.phone = c.phone
WHERE c.phone IS NULL
但我不确定它能否快速起作用
答案 1 :(得分:1)
在PHP中执行此操作不是更容易吗?
// The list of phone numbers to search
$listAll = array('123456', '456789', '789789', );
// Use data from $listAll to compose this query:
$query = "
SELECT DISTINCT phone
FROM contacts
WHERE phone IN ('123456', '456789', '789789')
AND deleted=0 AND user=1
";
// Run the query, get the list of phone numbers in $listFound
// Let's suppose it produces:
// $listFound = array('123456', '456789');
// Get the list of phone numbers that are missing from table
$listMissing = array_diff($listAll, $listFound);
答案 2 :(得分:0)
试试这个:
select DISTINCT phone
from contacts
WHERE (phone='123456' OR phone='456789' OR phone='789789')
AND deleted = 0
AND user = 1
AND found = 0
答案 3 :(得分:0)
试试这个:
SELECT distinct phone
FROM contacts
WHERE phone NOT IN (123456,456789) AND
deleted = 0 AND
user=1