English 中文(简体)
2. Mysql利用多种条件选择数据
原标题:Mysql select data using multiple conditions

我的表象

id   fid
20    53
23    53
53    53

Here I need to return true when my condition is something like .. where fid=53 and id in(20,23,53) and also I need to return false when....where fid=53 and id in(20,24,53). But the above condition not satisfy my requirement.Both queries returns same value(true).Please help me to go forward.

最佳回答

我认为,你再次要求的是,要找到与20,23,53 和如果不是的话,那是 que的 t。

这在结构中有两个共同的解决办法。

首先,我建议我为我的SQL:

SELECT t1.fid
FROM mytable t1
JOIN mytable t2 ON t1.fid = t2.fid
JOIN mytable t3 ON t1.fid = t2.fid
WHERE (t1.id, t2.id, t3.id) = (20,23,53);

这里还有另一种解决办法,即使用群体而不是自谋职业,但在我的SQL中往往更糟:

SELECT t.fid
FROM mytable t
WHERE t.id IN (20,23,53)
GROUP BY t.fid
HAVING COUNT(*) = 3;
问题回答

也许会是一个分局?

SELECT id, fid FROM table
WHERE fid = 53 AND
id IN ( SELECT id FROM table WHERE id IN(20,24,53))

According to your example conditions, its not possible with a single query :(. since where fid=53 and id in(20,23,53) return true for fid=53 and id =20,53 Similarly, where fid=53 and id in(20,24,53) is false for fid=53 and id =20,53

唯一的区别是id=23和id=24。 因此,你可以提出两个不同问题。 但有时他们会以同样数据换取真实和虚假的回报。

我认为,你需要再次思考你的问题。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签