1. 程式人生 > 其它 >sql 有記錄返回1,無記錄返回0

sql 有記錄返回1,無記錄返回0

技術標籤:資料庫Mysqlsql

方案一

只返回數字

SELECT DISTINCT IF(EXISTS(SELECT 1 FROM tb WHERE id=1), 1, 0) AS res FROM tb;

作為另一條sql賦值

SELECT
tb2.*,
IF(EXISTS(SELECT 1 FROM tb WHERE id=1), 1, 0) AS res
FROM tb2;

方案二

SELECT IF(COUNT(*), 1, 0) AS res FROM tb WHERE id = 1

--作為另一條sql賦值
SELECT
tb2.*,
(SELECT IF(COUNT(*), 1, 0) FROM tb WHERE id = 1) AS res
FROM
tb2;
	

OK.