mysql查询左连接和过滤

时间:2015-11-06 03:41:14

标签: mysql sql left-join where

SELECT
 TABLE_1.ID,
 IF(TABLE_2.show IS NULL,0,1) AS RESULT,
FROM
 TABLE_1
LEFT JOIN TABLE_2 ON 
 TABLE_1.ID=TABLE_2.ID AND DATE(DATE_ADD( TABLE_2.DATE, INTERVAL +10 DAY)) < NOW()
WHERE
 TABLE_1.ID='1';

我的问题:table_1.ID = table_2.ID

TABLE_2.DATE = 06-11-2015
NOW          = 07-11-2015

if TABLE_2.DATE < NOW() = true  (RESULT = 1)

TABLE_2.DATE = 06-11-2015
NOW          = 05-11-2015
if TABLE_2.DATE < NOW() = false (RESULT = 0)

but
have TABLE_1.ID
have TABLE_2.ID
  

我需要

     

1.position“date ok”,“table connection ok”= item old

     

2.position“date off”,“table connection ok”= item hidden

     

3.position“date ok”,“tablo connection no”= item new

但没有工作:(

1 个答案:

答案 0 :(得分:0)

SELECT
 TABLE_1.ID,
 IF(TABLE_2.show IS NULL,0,1) AS RESULT,
FROM
 TABLE_1
LEFT JOIN TABLE_2 ON 
 TABLE_1.ID=TABLE_2.ID AND DATE(DATE_ADD( TABLE_2.DATE, INTERVAL +10 DAY)) < NOW() AND TABLE_2.DATE IS NOT NULL
WHERE
 TABLE_1.ID='1';
  

1.position“date ok”,“table connection ok”= item old(它的工作)

     

2.position“date off”,“table connection ok”=隐藏项目(它不起作用)

     

3.position“date ok”,“tablo connection no”= item new(它的工作)

我的问题是2.职位