如何在MYSQL中将2个查询的结果作为1个输出

时间:2017-09-24 07:46:47

标签: mysql

我对MySQL有点新鲜,并浏览了其他答案,但无法让我的工作符合我的需要: 我通过PHP中的mysqli使用它

CREATE TEMPORARY TABLE IF NOT EXISTS 
  temp_table ( INDEX(ps_inv_id) ) 
AS (
 SELECT ps_inv_id,ps_custom_fields,ps_item_id,ps_member,ps_name,ps_active 
  FROM purchase_check  
  WHERE purchase_check.ps_member = '63' AND purchase_check.ps_active ='1' )UNION
  SELECT * 
  FROM temp_table   
  WHERE  ps_inv_id IN (SELECT MIN(ps_inv_id) FROM temp_table GROUP BY ps_name);

我尝试使用UNION可能是错的,但我会尝试解释我需要做什么:

现在对表格的解释:

  • ps_active只能是0或1;
  • ps_name可能因产品而异
  • ps_member,取决于会员的产品
  • ps_item_id,根据产品,可以是最多50种产品
  • ps_custom_fields,也与产品相关
  • ps_inv_id,1个唯一身份/交易

我的目标是:对于我正在查询的会员,我想要提取他当前所有的有效产品,但可能会有不止一个,在这种情况下,我只想要相同产品的较低发票ID

ps_inv_id   ps_custom_fields    ps_item_id  ps_member       ps_name     ps_active   G_name  
37          {"1":"0<2+0 bases>"}    1       54  INNOVATIVE Elite Casual     0       BP
142         {"1":"0<2+0 bases>"}    1       54  INNOVATIVE Elite Casual-VIP 1       BP
45          {"1":"0<2+0 bases>"}    1       55  INNOVATIVE Elite Casual     1       BP
115         {"1":"0<2+0 bases>"}    1       56  INNOVATIVE Elite Casual     1       BP
53          {"1":"0<2+0 bases>"}    1       57  INNOVATIVE Elite Casual     0       BP
44          {"1":"2"}               1       59  INNOVATIVE Elite Casual     0       BP
124         {"1":"1<2+1 bases>"}    1       59  INNOVATIVE Elite Casual-VIP 1       BP
25          {"1":"2"}               19      63  INNOVATIVE Elite Casual UC  0       BP
120         {"1":"7"}               1       63  INNOVATIVE Elite Casual-VIP 0       BP
123         {"1":"0<2+0 bases>"}    1       63  INNOVATIVE Elite Casual-VIP 1       BP
122         {"1":"5<2+5 bases>"}    1       63  INNOVATIVE Elite Casual-VIP 1       BP
38          {"1":"1<2+1 bases>"}    1       65  INNOVATIVE Elite Casual     1       BP
113         {"1":"0<2+0 bases>"}    1       67  INNOVATIVE Elite Casual     1       BP
51          {"1":"2<2+2 bases>"}    1       68  INNOVATIVE Elite Casual     1       BP
101         {"1":"1<2+1 bases>"}    1       69  INNOVATIVE Elite Casual     1       BP
56          {"1":"0<2+0 bases>"}    1       70  INNOVATIVE Elite Casual     1       BP
55          {"1":"0<2+0 bases>"}    1       74  INNOVATIVE Elite Casual     1       BP
26          {"1":"0<2+0 bases>"}    1       75  INNOVATIVE Elite Casual     0       BP
39          {"1":"0<2+0 bases>"}    1       76  INNOVATIVE Elite Casual     0       BP
94          {"1":"2"}               1       77  INNOVATIVE Elite Casual     0       BP
98          {"1":"0<2+0 bases>"}    1       77  INNOVATIVE Elite Casual     1       BP
22          {"1":"0<2+0 bases>"}    1       78  INNOVATIVE Elite Casual     1       BP
42          {"1":"0<2+0 bases>"}    1       79  INNOVATIVE Elite Casual     1       BP
57          {"1":"1<2+1 bases>"}    1       80  INNOVATIVE Elite Casual     0       BP
86          {"1":"1<2+1 bases>"}    1       80  INNOVATIVE Elite Casual     1       BP

所以,我基本上想做:

SELECT ps_inv_id,ps_custom_fields,ps_item_id,ps_member,ps_name,ps_active 
  FROM purchase_check  
  WHERE purchase_check.ps_member = '63' AND purchase_check.ps_active ='1'

得到结果:

ps_inv_id   ps_custom_fields    ps_item_id  ps_member   ps_name                     ps_active   
123         {"1":"0<2+0 bases>"}    1       63          INNOVATIVE Elite Casual-VIP     1
122         {"1":"5<2+5 bases>"}    1       63          INNOVATIVE Elite Casual-VIP     1

这个会员,63,只有一个活跃的产品类型,1,但根据发票中的2个相同的产品,inv 122和123,我只想要2,nr 122中的第一个,如果有更多的产品对于这个用户来说活跃的类型,我希望该成员的每种产品类型都有一张发票。

将它们放入临时表中,然后从临时表中,我想:

SELECT * 
  FROM temp_table   
  WHERE  ps_inv_id IN (SELECT MIN(ps_inv_id) FROM temp_table GROUP BY ps_name)

仅给我1个结果:

122         {"1":"5<2+5 bases>"}    1       63          INNOVATIVE Elite Casual-VIP     1

但是所有这些都是一个查询,正如我在开始时所尝试的那样。

至于MCVE ..这就像我能得到的一样完整,我可以根据需要添加更多解释,但是,通过代码和示例,我认为很清楚。

编辑: 如果我将它用作3个单独的查询:

 CREATE TABLE IF NOT EXISTS temp_table (INDEX(ps_inv_id) )AS 
SELECT ps_inv_id,ps_custom_fields,ps_item_id,ps_member,ps_name,ps_active FROM purchase_check WHERE purchase_check.ps_member = '63' AND purchase_check.ps_active ='1';


 SELECT * 
  FROM temp_table   
  WHERE  ps_inv_id IN (SELECT MIN(ps_inv_id) FROM temp_table GROUP BY ps_name);


DROP TABLE temp_table  ;

然后它工作正常,但表不是内存中的临时表... 一旦我将其更改为CREATE TEMPORARY TABLE,它告诉我temp_table不存在...

所以现在,它最接近我的工作答案

0 个答案:

没有答案