简体   繁体   English

无法从MySQL查询中获得所需的结果

[英]Can't get desired result from MySQL query

I am trying to write a query in MySQL with output in PHP, but it's proving rather difficult. 我试图用PHP中的输出在MySQL中编写一个查询,但事实证明它很难。

I have a table with 3 columns (yes I know the database layout for this isn't the best, but I can't change this, the client has other software using this db with this exact layout) 我有一个包含3列的表(是的我知道这个数据库的布局不是最好的,但是我不能改变它,客户端有其他软件使用这个db这个确切的布局)

Amount - Product - Package 
2      - Apple   - Fruitbasket1
1      - Pear    - Fruitbasket1
5      - Grape   - Fruitbasket1
2      - Apple   - Fruitbasket2
1      - Pear    - Fruitbasket2
9      - Banana  - Fruitbasket2

So far I came up with this: 到目前为止,我想出了这个:

SELECT package 
FROM `data` 
WHERE package IN 
(
  SELECT package 
  FROM `data` 
  WHERE product = 'apple' AND amount = '2'
) 
AND package IN 
(
  SELECT package 
  FROM `data` 
  WHERE product = 'pear' AND amount ='1'
) 
AND package IN 
(
  SELECT package 
  FROM `data` 
  WHERE product = 'grape' AND amount = '5'
) 
LIMIT 0,1

The issue is that when the customer would fill out "apple" and "2" on the form, it would return all the baskets containing 2 apples. 问题是,当客户在表单上填写“apple”和“2”时,它将返回包含2个苹果的所有篮子。 What I need is a statement that will only accept the exact products and their amounts to return the one basket those ingredients belong to, or nothing at all. 我需要的是一份声明,它只接受确切的产品及其数量,以返回那些成分所属的一篮子,或者根本没有。

So even though 1 pear and 2 apples are in Fruitbasket1, it should not give Fruitbasket1 as a result if the other part, namely 5 grapes, are not specified by the customer. 因此,即使1个梨和2个苹果在Fruitbasket1中,如果客户没有指定其他部分,即5个葡萄,它也不应该给Fruitbasket1。

this query will return all packages where the only product is 2 Apples 此查询将返回所有包,其中唯一的产品是2个苹果

select package from `data`
group by package
having count(case when amount = 2 and product = 'Apple' then 1 end) = 1
and count(*) = 1

if you want 1 pear and 2 apples for example 如果你想要1个梨和2个苹果

select package from `data`
group by package
having count(
  case 
    when (amount = 2 and product = 'Apple') or (amount = 1 and product = 'Pear') 
    then 1 
  end) = 2
and count(*) = 2

you could also write this as 你也可以这样写

select package from `data`
group by package
having count(case when amount = 2 and product = 'Apple' then 1 end) = 1
   and count(case when amount = 1 and product = 'Pear' then 1 end) = 1
   and count(*) = 2

I only have Oracle handy so you may have to adjust the syntax somewhat, but following your original style I've added a "not in" section to make sure the basket contains only the fruits you are looking for. 我只有Oracle方便,所以你可能需要稍微调整一下语法,但是按照你的原始风格,我添加了一个“不在”部分,以确保篮子只包含你正在寻找的成果。 Here are some inserts with a couple of examples: 以下是一些带有几个示例的插入:

create table data (amount integer, product varchar(12), package varchar(16));

insert into data values (2, 'Apple', 'Fruitbasket1');
insert into data values (1, 'Pear', 'Fruitbasket1');
insert into data values (5, 'Grape', 'Fruitbasket1');
insert into data values (2, 'Apple', 'Fruitbasket2');
insert into data values (1, 'Pear', 'Fruitbasket2');
insert into data values (9, 'Banana', 'Fruitbasket2');
insert into data values (2, 'Apple', 'Fruitbasket3');
insert into data values (2, 'Apple', 'Fruitbasket4');
insert into data values (1, 'Pear', 'Fruitbasket4');


SELECT package 
FROM data 
WHERE package IN 
(
  SELECT package 
  from data 
  WHERE product = 'Apple' AND amount = '2'
)
and package not in
(
  SELECT package 
  from data 
  WHERE product <> 'Apple'
)
group by package
;


SELECT package 
FROM data 
WHERE package IN 
(
  SELECT package 
  from data 
  WHERE product = 'Apple' AND amount = '2'
) 
AND package IN 
(
  SELECT package 
  FROM data 
  WHERE product = 'Pear' AND amount ='1'
) 
and package not in
(
  SELECT package 
  from data 
  WHERE product <> 'Apple' and product <> 'Pear'
)
group by package
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM