Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql多对多查询,其中包含来自其他表的位置_Mysql_String_Indexing_Many To Many_Where - Fatal编程技术网

mysql多对多查询,其中包含来自其他表的位置

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

这应该很简单,但我的SQL能力仍然很弱。 我在MySQL中为多对多查询设置了一个中间表,我们称之为“item_packs”,将“packs”表与“items”表链接起来。 我想列出输入的“包装名称”与“包装”表中的名称匹配的所有“项目”

packs table

packindex | packname (varchar) ------------------------------ 1 | todayspack 2 | anotherpack

items table

itemindex | itemname (varchar) ------------------------------ 1 | firstitem 2 | anotheritem

item_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';