mysql语句连接另一个表,需要联接2个表,但MySQL中另一个表中的某些行除外

I'm not so expert. I need to join 2 tables but except a specific rows whose status is 'Del Edge'. And also duplicate rows are not allowed. I also try to write an query but I think it's not in correct form. For example user 't' log in and he search a name 'Bush', so I need only those data. Any help would be appreciated. Here are the example tables:

links Table:

Id(PK) source target freq

1 Bush Fisher 1

2 Lamburt Bush 6

3 Sam Bush 3

4 Fisher Sam 7

5 Bush Dalai 4

logs Table:

username Id (FK) source target frequency status

t 5 Bush Dalai 4 Add Node

m 8 Dalai Pit 5 Del Edge

t 3 Sam Bush 3 Del Edge

Joining Table should be:

source target frequency

Bush Fisher 1

Lamburt Bush 6

Bush Dalai 4

My Query:

"SELECT source, target, frequency from links, logs

where (links.source=Bush || links.target= Bush) &&

where not exists

(SELECT source, target, frequency FROM logs

WHERE (links.id = logs.id && logs.status=Del Edge)";

解决方案

The following should do the trick!

SELECT DISTINCT k.source,

k.target,

k.frequency

FROM links k

LEFT JOIN logs g

ON g.id = k.id

WHERE IFNULL(status, '') != 'Del Edge'

AND 'Bush' IN( k.source, k.target )

Hope this helps!

Also, the following fiddle demonstrates that the above answer is, in fact, correct: http://sqlfiddle.com/#!2/9753f/5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值