简体   繁体   中英

Mysql Query - Get row value based on column name in a different table

I have 2 tables. One for inventory and one for information about inventory items.

The info table caries much more information about a website and I would like to keep all the info in one table.

INVENTORY TABLE

UserID--ITEM1--ITEM2--ITEM3

key1--qty--qty--qty

key2--qty--qty--qty

Table2 structure "info table"

  ItemClass  --   ItemID   --  Info

   CLASS1    --   ITEM1    --  "best item ever info"

   CLASS1    --   ITEM2    --   "worst item ever info"

   CLASS9    --   ITEM3    --  "some other item info"

I'm trying to get a single query to grab CLASS1 rows where ItemID matches a column name in Table1 if the qty is above 0 and the key = $a

Any help generating this query would be a life saver. I do not want to redo my inventory schema. Thanks for looking!

SELECT info.*
FROM info
JOIN Inventory ON (ItemID = 'ITEM1' AND ITEM1 > 0)
               OR (ItemID = 'ITEM2' AND ITEM2 > 0)
               OR (ItemID = 'ITEM3' AND ITEM3 > 0)
WHERE info.ItemClass = 'CLASS1'
AND Inventory.userID = $a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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