如何根据mysql中另一行的值选择或不选择行 [英] How to select or not select a row depending on the value of another row in mysql
本文介绍了如何根据mysql中另一行的值选择或不选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
+ ---------- + ------------ +
|名字|代码|
+ ---------- + ------------ +
|蓬松| 000 |
|爪子| 217 |
|巴菲| 598 |
|方| 000 |
|鲍泽尔| 687 |
| Chirpy | 587 |
|惠斯勒| 258 |
|苗条| 298 |
|蓬松| 168 |
+ ---------- + ------------ +
这是我的问题...在上表中,代码列的默认值是000.在名称列中,Fluffy有2个代码,一个是默认值000&另一个168选择时,如果蓬松的东西不是默认代码,我选择那个&其他行留下一行默认为Fluffy.Any方式用mysql IF或其他任何方式吗?希望我的问题很明确。
解决方案
这实际上真的很讨厌! :笑:
但是可以做到。
我怀疑这是否是最有效的方式,但是...
SELECT 名称,代码 FROM MyTable m
WHERE 代码<> 0
OR ( SELECT COUNT (代码) FROM MyTable t
WHERE m.name = t.name)= 1
+----------+------------+ | name | code | +----------+------------+ | Fluffy | 000 | | Claws | 217 | | Buffy | 598 | | Fang | 000 | | Bowser | 687 | | Chirpy | 587 | | Whistler | 258 | | Slim | 298 | | Fluffy | 168 | +----------+------------+
Here's my problem... in the above table the default value for column "code" is 000.In the column "name", Fluffy has 2 code one is the default 000 & the other one 168 When selecting, if fluffy has something other than the default code i've to select that & other rows leaving out the one row with default for Fluffy.Any way to do that with mysql IF or any other ? Hope my question is clear.
解决方案
That's actually really nasty! :laugh:
But it can be done.
I doubt if this is the most efficient way, but...
SELECT name, code FROM MyTable m WHERE code<>0 OR (SELECT COUNT(code) FROM MyTable t WHERE m.name = t.name)=1
这篇关于如何根据mysql中另一行的值选择或不选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文