私はMYSQLを初めて使い、2つのテーブルを結合して一意の製品行を返すクエリを作成するのに苦労しています。2つのテーブルとGROUPを共通の列で結合する
TABLEproduct
:
╔══════════════════════════════╦══════════╦════════════╦════════════════════╗
║ ref ║ brand ║ mpn ║ sku ║
╠══════════════════════════════╬══════════╬════════════╬════════════════════╣
║ 0001___DOGICLON___912-101242 ║ DOGICLON ║ 912-101242 ║ 000000000001082649 ║
║ 0002___DOGICLON___912-101242 ║ DOGICLON ║ 912-101242 ║ 912-101242 ║
║ 0003___Dogiclon___912-101242 ║ Dogiclon ║ 912-101242 ║ 912-101242(R400) ║
║ 0005___Dogiclon___912-101242 ║ Dogiclon ║ 912-101242 ║ MILT-R400 ║
╚══════════════════════════════╩══════════╩════════════╩════════════════════╝
TABLEinventory
:
╔══════════════════════════════╦═══════╦═════════╦══════════╗
║ ref ║ scost ║ instock ║ location ║
╠══════════════════════════════╬═══════╬═════════╬══════════╣
║ 0001___DOGICLON___912-101242 ║ 53.68 ║ 24 ║ WA ║
║ 0001___DOGICLON___912-101242 ║ 53.68 ║ 0 ║ CA ║
║ 0002___DOGICLON___912-101242 ║ 61.00 ║ 121 ║ WA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 100 ║ WA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 0 ║ NY ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 20 ║ MA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 2 ║ CA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ IN ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ MA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ WA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ NY ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 2 ║ CA ║
╚══════════════════════════════╩═══════╩═════════╩══════════╝
私はpseduoコードは次のようになり推測:
SHOW all products
WHERE
instock (any location) > 0 AND
(cost > 10 AND cost < 2000)
ORDER BY
cost asc
ノート:
╔══════════╦══════════╦════════════╦══════════════╦══════════════╦═══════════════════════════╗
║ ref ║ brand ║ mpn ║ sku ║ scost ║ instock ║
╠══════════╬══════════╬════════════╬══════════════╬══════════════╬═══════════════════════════╣
║ whatever ║ Dogiclon ║ 912-101242 ║ based on ref ║ based on ref ║ based on ref and location ║
╚══════════╩══════════╩════════════╩══════════════╩══════════════╩═══════════════════════════╝
これは何です:
ref
sがbrand
とmpn
検索が鈍感な
期待される結果 ケースにする必要があるサプライヤーごとに一意です私はtrですying:
SELECT DISTINCT
product.ref,
product.brand,
inventory.scost,
inventory.instock
FROM
product
JOIN inventory ON inventory.ref = product.ref
WHERE
inventory.instock > 1
AND (app.inventory.scost >= 10 AND app.inventory.scost <= 2000)
GROUP BY
product.ref
予期せぬ結果がデータに含まれているため、サンプルデータと期待される結果を表示する必要があります。私の推測では、1つの製品refに関連付けられたインベントリテーブル内に複数のレコードがあることです。 – Shadow
@ Shadowああ、商品には重複した参考文献はありませんが、在庫は – 3zzy