如何组合不同条件的不同查询

时间:2015-11-01 20:26:49

标签: mysql

我有三张桌子:a,b和c
a:(q,r,s),b:(q,r,t),c:(q,r,u,v)
我想做一个这样的查询:

    select q,r from a where s="whatever", q="whatever"
    select q,r from b where t="whatever", q="whatever"
    select q,r from c where u="whatever", q="whatever"

我希望结果在一个查询中,按r排序。这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用SQL UNION运算符。它用于组合2个或更多SELECT语句的结果集。它删除了各种SELECT语句之间的重复行。

UNION中的每个SELECT语句必须在具有相似数据类型的结果集中具有相同数量的字段。

示例:

> gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes
> -Wstrict-prototypes -Wold-style-definition -Werror   -o working working.c  dave@hellion:/home/home/dave/src/ringbuffer/src$ ./working 
>    2.2    4.3    6.0    8.0   10.0   12.0 
>    3.2    6.3    9.0   12.0   15.0   18.0 
>    4.2    8.3   12.0   16.0   20.0   24.0 
>    5.2   10.3   15.0   20.0   25.0   30.0 
>    6.2   12.3   18.0   24.0   30.0   36.0 
>    7.2   14.3   21.0   28.0   35.0   42.0 
>    8.2   16.3   24.0   32.0   40.0   48.0 
>    9.2   18.3   27.0   36.0   45.0   54.0 
>   10.2   20.3   30.0   40.0   50.0   60.0 
>   11.2   22.3   33.0   44.0   55.0   66.0 
>   12.2   24.3   36.0   48.0   60.0   72.0 
>   13.2   26.3   39.0   52.0   65.0   78.0 
>   14.2   28.3   42.0   56.0   70.0   84.0 
>   15.2   30.3   45.0   60.0   75.0   90.0 
>   16.2   32.3   48.0   64.0   80.0   96.0 
>   17.2   34.3   51.0   68.0   85.0  102.0 
>   18.2   36.3   54.0   72.0   90.0  108.0 
>   19.2   38.3   57.0   76.0   95.0  114.0 
>   20.2   40.3   60.0   80.0  100.0  120.0 
>   21.2   42.3   63.0   84.0  105.0  126.0 
>   22.2   44.3   66.0   88.0  110.0  132.0 
>   23.2   46.3   69.0   92.0  115.0  138.0 
>   24.2   48.3   72.0   96.0  120.0  144.0 
>   25.2   50.3   75.0  100.0  125.0  150.0 
>   26.2   52.3   78.0  104.0  130.0  156.0 
>   27.2   54.3   81.0  108.0  135.0  162.0 
>   28.2   56.3   84.0  112.0  140.0  168.0 
>   29.2   58.3   87.0  116.0  145.0  174.0 
>   30.2   60.3   90.0  120.0  150.0  180.0 
>   31.2   62.3   93.0  124.0  155.0  186.0 
>   32.2   64.3   96.0  128.0  160.0  192.0 
>   33.2   66.3   99.0  132.0  165.0  198.0 
>   34.2   68.3  102.0  136.0  170.0  204.0 
>   35.2   70.3  105.0  140.0  175.0  210.0 
>   36.2   72.3  108.0  144.0  180.0  216.0 
>   37.2   74.3  111.0  148.0  185.0  222.0 
>   38.2   76.3  114.0  152.0  190.0  228.0 
>   39.2   78.3  117.0  156.0  195.0  234.0 
>   40.2   80.3  120.0  160.0  200.0  240.0 
>   41.2   82.3  123.0  164.0  205.0  246.0 
>   42.2   84.3  126.0  168.0  210.0  252.0 
>   43.2   86.3  129.0  172.0  215.0  258.0 
>   44.2   88.3  132.0  176.0  220.0  264.0 
>   45.2   90.3  135.0  180.0  225.0  270.0 
>   46.2   92.3  138.0  184.0  230.0  276.0 
>   47.2   94.3  141.0  188.0  235.0  282.0 
>   48.2   96.3  144.0  192.0  240.0  288.0 
>   49.2   98.3  147.0  196.0  245.0  294.0 
>   50.2  100.3  150.0  200.0  250.0  300.0 
>   51.2  102.3  153.0  204.0  255.0  306.0 
>    2.2    4.3    6.0    8.0   10.0   12.0 
>    3.2    6.3    9.0   12.0   15.0   18.0 
>    4.2    8.3   12.0   16.0   20.0   24.0 
>    5.2   10.3   15.0   20.0   25.0   30.0 
>    6.2   12.3   18.0   24.0   30.0   36.0 
>    7.2   14.3   21.0   28.0   35.0   42.0 
>    8.2   16.3   24.0   32.0   40.0   48.0 
>    9.2   18.3   27.0   36.0   45.0   54.0 
>   10.2   20.3   30.0   40.0   50.0   60.0 
>   11.2   22.3   33.0   44.0   55.0   66.0 
>   12.2   24.3   36.0   48.0   60.0   72.0 
>   13.2   26.3   39.0   52.0   65.0   78.0 
>   14.2   28.3   42.0   56.0   70.0   84.0 
>   15.2   30.3   45.0   60.0   75.0   90.0 
>   16.2   32.3   48.0   64.0   80.0   96.0 
>   17.2   34.3   51.0   68.0   85.0  102.0 
>   18.2   36.3   54.0   72.0   90.0  108.0 
>   19.2   38.3   57.0   76.0   95.0  114.0 
>   20.2   40.3   60.0   80.0  100.0  120.0 
>   21.2   42.3   63.0   84.0  105.0  126.0 
>   22.2   44.3   66.0   88.0  110.0  132.0 
>   23.2   46.3   69.0   92.0  115.0  138.0 
>   24.2   48.3   72.0   96.0  120.0  144.0 
>   25.2   50.3   75.0  100.0  125.0  150.0 
>   26.2   52.3   78.0  104.0  130.0  156.0 
>   27.2   54.3   81.0  108.0  135.0  162.0 
>   28.2   56.3   84.0  112.0  140.0  168.0 
>   29.2   58.3   87.0  116.0  145.0  174.0 
>   30.2   60.3   90.0  120.0  150.0  180.0 
>   31.2   62.3   93.0  124.0  155.0  186.0 
>   32.2   64.3   96.0  128.0  160.0  192.0 
>   33.2   66.3   99.0  132.0  165.0  198.0 
>   34.2   68.3  102.0  136.0  170.0  204.0 
>   35.2   70.3  105.0  140.0  175.0  210.0 
>   36.2   72.3  108.0  144.0  180.0  216.0 
>   37.2   74.3  111.0  148.0  185.0  222.0 
>   38.2   76.3  114.0  152.0  190.0  228.0 
>   39.2   78.3  117.0  156.0  195.0  234.0 
>   40.2   80.3  120.0  160.0  200.0  240.0 
>   41.2   82.3  123.0  164.0  205.0  246.0 
>   42.2   84.3  126.0  168.0  210.0  252.0 
>   43.2   86.3  129.0  172.0  215.0  258.0 
>   44.2   88.3  132.0  176.0  220.0  264.0 
>   45.2   90.3  135.0  180.0  225.0  270.0 
>   46.2   92.3  138.0  184.0  230.0  276.0 
>   47.2   94.3  141.0  188.0  235.0  282.0 
>   48.2   96.3  144.0  192.0  240.0  288.0 
>   49.2   98.3  147.0  196.0  245.0  294.0 
>   50.2  100.3  150.0  200.0  250.0  300.0 
>   51.2  102.3  153.0  204.0  255.0  306.0

您可以使用UNION ALL而不是UNION。它们之间的区别在于 UNION ALL 不会删除重复的行。