0

我有以下表格和数据

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

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

我得到了表 V 中未使用的 d_id,我的查询如下

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
4

1 回答 1

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
于 2013-10-09T09:10:07.930 回答