mysql多对多查询,其中包含来自其他表的位置
这应该很简单,但我的SQL能力仍然很弱。 我在MySQL中为多对多查询设置了一个中间表,我们称之为“item_packs”,将“packs”表与“items”表链接起来。 我想列出输入的“包装名称”与“包装”表中的名称匹配的所有“项目”mysql多对多查询,其中包含来自其他表的位置,mysql,string,indexing,many-to-many,where,Mysql,String,Indexing,Many To Many,Where,这应该很简单,但我的SQL能力仍然很弱。 我在MySQL中为多对多查询设置了一个中间表,我们称之为“item_packs”,将“packs”表与“items”表链接起来。 我想列出输入的“包装名称”与“包装”表中的名称匹配的所有“项目” packs tablepackindex | packname (varchar) ------------------------------ 1 | todayspack 2 | anotherpack items
packs table
packindex | packname (varchar) ------------------------------ 1 | todayspack 2 | anotherpackitems table
itemindex | itemname (varchar) ------------------------------ 1 | firstitem 2 | anotheritemitem_packs table
item | pack ------------------------------ 1 | 2 2 | 2如果我输入了包索引(1),它会起作用,但我需要让该值来自“packs”表中的索引,其中包名与输入的包名(todayspack)匹配 那么您想将名称与某个字符串进行比较?您的查询看起来不错,请尝试以下操作:
SELECT c.*
FROM items c
JOIN item_packs j on j.item = c.itemindex
JOIN packs t on j.pack = t.packindex
where j.packname='todayspack'
SELECT c.*
FROM items c
JOIN item_packs j on j.item = c.itemindex
JOIN packs t on j.pack = t.packindex
where j.packname='todayspack'
SELECT c.*
FROM items c
JOIN item_packs j on j.item = c.itemindex
JOIN packs t on j.pack = t.packindex
where t.packname = 'todayspack';