Mysql-在特定条件下从两列中选择

时间:2017-07-12 06:48:03

标签: php mysql count

我有两张桌子Table1&表2通过xid和Yid相互连接。

表1

xid(int)    category(varchar)       subcat(varchar)

1           Venue                   Resort
2           Venue                   Resort
3           Venue                   Resort
4           Venue                   Resort
5           Venue                   Resort
6           Venue                   Resort
7           Venue                   Banquet Hall
8           Venue                   Resort
9           Apparel                 Designer

表2

Yid(int)    minp(varchar)           maxp(varchar)

1           800                     1000
2           900                     1200
3                                   1100
4           1300                    1600
5                                   1800
6           800                     2000
7           800                     1000
8           1500                    2100
9           15000                   60000

我将这些供应商分为三类,第一类供应商的价格低于卢比。 1000.第二类是针对价格低于1500且超过1000且价格高于1500的供应商进入第三类。目前我只是将价格与maxp进行比较,并为我工作。

我用于与maxp进行比较的查询是

SELECT xid from Table1,Table2 WHERE
Table1.xid=Table2.Yid 
AND category='venue' 
AND subcat='Resort' 
AND maxp<=1000

和我得到的输出是

xid

1
7

现在我想比较价格与minp和maxp我想要计算并获取所有那些xid,其中minp或maxp中的任何一个小于1000,介于1000到1500之间,超过1500.

期望输出低于Rs。 1000

xid

1
2
6
7

期望输出超过Rs。 1000并且小于等于1500

xid

2
3
4
8

注意:由于某些原因,我无法将minp和maxp的数据类型从varchar更改为int或long,如果minp为空则不能与之进行比较。

提前致谢...

3 个答案:

答案 0 :(得分:0)

您需要根据预期结果检入两列。

SELECT xid 
from Table1 
INNER JOIN Table2 
        ON Table1.xid=Table2.Yid 
Where category='venue' 
AND subcat='Resort' 
AND (minp<=1000 or minp<=1000)
and minp <> ''

寻找卢比。 1000并且小于等于1500

SELECT xid 
from Table1 
INNER JOIN Table2 
        ON Table1.xid=Table2.Yid 
Where category='venue' 
AND subcat='Resort' 
AND (minp > 1000 or maxp > 1000) and (minp <= 1500 or maxp <= 1500)
and minp <> ''

注意:开始使用INNER JOIN语法而不是旧式逗号分隔的连接

答案 1 :(得分:0)

我认为你可以使用这样的东西:

   SELECT xid , CATEGORY, SUBCAT, minp, maxp
    , CASE WHEN COALESCE(minp,0)<=1000  OR COALESCE(maxp,0)<=1000 THEN 1 ELSE 0 END AS CAT1
    , CASE WHEN (COALESCE(minp,0)>1000 AND COALESCE(minp,0)<=1500) OR (COALESCE(maxp,0)>1000 AND COALESCE(maxp,0)<=1500) THEN 1 ELSE 0 END AS CAT2
    , CASE WHEN COALESCE(minp,0)>1500 OR COALESCE(maxp,0)>1500 THEN 1 ELSE 0 END AS CAT3                        
from Table1 A
INNER JOIN Table2 B ON A.xid=B.Yid 
;

输出:

xid CATEGORY SUBCAT   minp  maxp    CAT1    CAT2    CAT3
1   Venue   Resort    800   1000    1   0   0
2   Venue   Resort    900   1200    1   1   0
3   Venue   Resort    NULL  1100    1   1   0
4   Venue   Resort    1300  1600    0   1   1
5   Venue   Resort    NULL  1800    1   0   1
6   Venue   Resort    800    2000   1   0   1
7   Venue   BanquetH  800   1000    1   0   0
8   Venue   Resort    1500  2100    0   1   1
9   Apparel Designer 15000  60000   0   0   1

要为您可以使用的每个类别添加计数:

SELECT * 
FROM 
 (SELECT xid , CATEGORY, SUBCAT, minp, maxp
    , CASE WHEN COALESCE(minp,0)<=1000  OR COALESCE(maxp,0)<=1000 THEN 1 ELSE 0 END AS CAT1
    , CASE WHEN (COALESCE(minp,0)>1000 AND COALESCE(minp,0)<=1500) OR (COALESCE(maxp,0)>1000 AND COALESCE(maxp,0)<=1500) THEN 1 ELSE 0 END AS CAT2
    , CASE WHEN COALESCE(minp,0)>1500 OR COALESCE(maxp,0)>1500 THEN 1 ELSE 0 END AS CAT3                    
from Table1 A
INNER JOIN Table2 B ON A.xid=B.Yid ) X
CROSS JOIN (SELECT SUM(CASE WHEN CAT1=1 THEN 1 ELSE 0 END) AS CAT1_RC
                ,SUM(CASE WHEN CAT2=1 THEN 1 ELSE 0 END) AS CAT2_RC
                , SUM(CASE WHEN CAT3=1 THEN 1 ELSE 0 END) AS CAT3_RC
              FROM (SELECT xid , CATEGORY, SUBCAT, minp, maxp
                     , CASE WHEN COALESCE(minp,0)<=1000  OR COALESCE(maxp,0)<=1000 THEN 1 ELSE 0 END AS CAT1
                     , CASE WHEN (COALESCE(minp,0)>1000 AND COALESCE(minp,0)<=1500) OR (COALESCE(maxp,0)>1000 AND COALESCE(maxp,0)<=1500) THEN 1 ELSE 0 END AS CAT2
                     , CASE WHEN COALESCE(minp,0)>1500 OR COALESCE(maxp,0)>1500 THEN 1 ELSE 0 END AS CAT3                   
                     from Table1 A
                     INNER JOIN Table2 B ON A.xid=B.Yid) X1
              ) Y
              ;

输出:

+-----+----------+-----------+-------+-------+------+------+------+---------+---------+---------+
| xid | CATEGORY |  SUBCAT   | minp  | maxp  | CAT1 | CAT2 | CAT3 | CAT1_RC | CAT2_RC | CAT3_RC |
+-----+----------+-----------+-------+-------+------+------+------+---------+---------+---------+
|   1 | Venue    | Resort    | 800   |  1000 |    1 |    0 |    0 |       6 |       4 |       5 |
|   2 | Venue    | Resort    | 900   |  1200 |    1 |    1 |    0 |       6 |       4 |       5 |
|   3 | Venue    | Resort    | NULL  |  1100 |    1 |    1 |    0 |       6 |       4 |       5 |
|   4 | Venue    | Resort    | 1300  |  1600 |    0 |    1 |    1 |       6 |       4 |       5 |
|   5 | Venue    | Resort    | NULL  |  1800 |    1 |    0 |    1 |       6 |       4 |       5 |
|   6 | Venue    | Resort    | 800   |  2000 |    1 |    0 |    1 |       6 |       4 |       5 |
|   7 | Venue    | Banquet H | 800   |  1000 |    1 |    0 |    0 |       6 |       4 |       5 |
|   8 | Venue    | Resort    | 1500  |  2100 |    0 |    1 |    1 |       6 |       4 |       5 |
|   9 | Apparel  | Designer  | 15000 | 60000 |    0 |    0 |    1 |       6 |       4 |       5 |
+-----+----------+-----------+-------+-------+------+------+------+---------+---------+---------+

答案 2 :(得分:0)

我认为你需要尝试这个。

1.您想要将价格与minp和maxp进行比较我想要计算并获取所有那些xid,其中minp或maxp中的任何一个小于1000,介于1000到1500之间,超过1500.

  

从表1中选择t1.xid t1,表2 t2 WHERE t1.xid = t2.Yid AND   t1.category =&#39;场地&#39;和t1.subcat =&#39;度假村&#39; AND(t2.maxp <= 1000或   t2.minp&LT; = 1000)

  1. 输出超过Rs。 1000并且小于等于1500
  2.   

    从表1中选择t1.xid t1,表2 t2 WHERE t1.xid = t2.Yid AND   t1.category =&#39;场地&#39;和t1.subcat =&#39;度假村&#39; AND((t2.maxp> = 1000和   t2.maxp&lt; = 1500)OR(t2.minp&gt; = 1000且t2.minp&lt; = 1500)