mysql左连接查询为null

时间:2013-10-09 08:57:20

标签: mysql sql left-join isnull

我有以下表格和数据

Table D
d_id
-----------
1   
2   
3

Table V
v_id            d_id        
----------- -----------
1           1
2           2

我得到的d_id在表V中没有使用,我的查询如下

SELECT distinct(d.d_id) AS d
     , v.v_id  AS v
FROM
        D  LEFT  JOIN  V
  ON   ( d.d_id     =    v.v_id  )
where   v.v_id IS  NULL

d_id
-------------
3

当我将值(例如:d.d_id = 2)传递给查询时,我希望得到以下格式的结果

d_id
-------------
    2
    3

1 个答案:

答案 0 :(得分:1)

不同(d.d_id)

select distinct不是一个函数,虽然使用这些括号不会导致语法错误,但它们对select distinct的行为完全没有影响;所以所有列将被评估以产生一组不同的值。

SELECT /* DISTINCT is this really needed? Don't use it unless needed */
       d.d_id AS d
     , v.v_id AS v
FROM D
LEFT JOIN V ON (d.d_id = v.v_id)
WHERE v.v_id IS NULL
OR d.d_id = 1

-- OR v.d_id = 1 -- not clear on which d_id you want to pass