JOIN后仅从一个表返回数据

时间:2017-02-22 13:08:39

标签: sql postgresql

我有表A和B,我想只获取表A中的行,其中user_id也在表B中,并且表B中的过期日期小于CURRENT_TIME + 6个月。

SELECT * from u_uzivatele JOIN bw_paid_orders
      ON u_uzivatele.id = bw_paid_orders.user_id 
WHERE bw_paid_orders.active_thru < NOW() + interval '6 months'

此查询执行此操作,但它返回A和B中的所有列,我只想从A中获取列。

3 个答案:

答案 0 :(得分:1)

你可以这样做:

SELECT u_uzivatele.* FROM u_uzivatele 
JOIN bw_paid_orders ON u_uzivatele.id = bw_paid_orders.user_id 
WHERE bw_paid_orders.active_thru < NOW() + interval '6 months'

u_uzivatele获取所有列。 或者如果你想要那里的列,则传递bw_paid_orders表。

答案 1 :(得分:1)

SELECT a.* 
from u_uzivatele as a
JOIN bw_paid_orders b ON a.id = b.user_id 
WHERE b.active_thru < NOW() + interval '6 months

答案 2 :(得分:0)

试试这个

SELECT a.* FROM u_uzivatele a JOIN bw_paid_orders b
      ON a.id = b.user_id 
WHERE b.active_thru < NOW() + INTERVAL '6 months'