2017-11-12 10 views
0

は、私は、次のコードを持っている:排他的なINの使い方は?

SELECT stores_tb.stores, sum(products_tb_tb.prices) 
     from products_tb 
     inner join stores_tb 
     on products_tb.id_store = stores_tb.id_store 
     where products_tb.barcode IN ($barcodes) 
     group by stores_tb.stores 
     order by sum(products_tb.prices) 

なので、私はMySQLを実行するPHPファイルにAJAXを経由して受信$barcodes(すでに文字列に変換)の配列。

事は、INは、配列値のそれぞれについて、ORを使用して、包括的であるということであるSELECTに必要な店舗の場合は、1つ、1つを持っていることを意味し、すべてではなく、アレイ内のバーコードの、それが表示されます。

私はそれが配列で渡されたバーコードのすべて、代わりに ANDを使用してのequvilantを持っている唯一の店舗を返しますする INのような関数(または IN機能を使用する方法)があるかどうかを知りたい

各アレイ値に対してORの値を持ちます。

答えて

0

使用することができ、アレイとIN句:

select s.stores, sum(p.prices) 
from products_tb p join 
    stores_tb s 
    on p.id_store = s.id_store 
where p.barcode IN ($barcodes) 
group by s.stores 
having count(distinct p.barcode) = $n -- the number of codes that need to match 
order by sum(p.prices); 

$nの値は、$barcodesリストの長さです(厳密には、その中の一意の項目の数)。

+0

'' count(distinct p.id_store)= $ n''は、 '' count(distinct p.barcode)= $ n''を意味します。そうすれば、それは働いた。ありがとうございました! @allansanfer。 – allansanfer

+0

。 。はい、それは本当にすべきです。 –

0

の代わりに、あなたがhaving句でこれを行うことができます副選択とALLオペレータ

SELECT stores_tb.stores, sum(products_tb_tb.prices) 
    from products_tb 
    inner join stores_tb 
    on products_tb.id_store = stores_tb.id_store 
    where products_tb.barcode = ALL (
     select barcode from my_table) 
    ) 
    group by stores_tb.stores 
    order by sum(products_tb.prices) 

https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html

https://dev.mysql.com/doc/refman/5.7/en/all-subqueries.html

関連する問題