Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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_Sql - Fatal编程技术网

Mysql 从表中选择一个有限的编号,但所有编号均从联接表中连接

Mysql 从表中选择一个有限的编号,但所有编号均从联接表中连接,mysql,sql,Mysql,Sql,我正在从事一个SQL项目,并试图实现以下目标: 我有一张表叫配方,一张表叫产品,还有一张表叫RecipeProducts。所有配方都有自己的ID,所有产品都有自己的ID。Recipeproducts中存储的recipeID和ProductID用于加入它们 我已经有以下问题: select recipeproducts.recipeID, recipes.recipeName, recipeproducts.productID, products.produ

我正在从事一个SQL项目,并试图实现以下目标:

我有一张表叫配方,一张表叫产品,还有一张表叫RecipeProducts。所有配方都有自己的ID,所有产品都有自己的ID。Recipeproducts中存储的recipeID和ProductID用于加入它们

我已经有以下问题:

select 
    recipeproducts.recipeID, 
    recipes.recipeName, 
    recipeproducts.productID, 
    products.productName, 
    products.quantity AS perverpakking, 
    recipeproducts.productQuantity AS nodig 
FROM 
    recipeproducts, 
    recipes, 
    products 
WHERE 
    recipeproducts.recipeID = recipes.recipeID AND
    recipeproducts.productID = products.productID
我只想得到(例数)3个随机配方,但所有相应的产品

项目的其余部分基于PHP


我该怎么做呢?

很遗憾,您在
FROM
子句中学会了使用带逗号的SQL。您确实应该使用正确、明确、标准的
JOIN
语法

要想做你想做的事,请在加入之前先品尝食谱:

select r.recipeID, r.recipeName, p.productID, p.productName,
       p.quantity AS perverpakking, rp.productQuantity AS nodig
FROM (SELECT r.*
      FROM recipes r
      ORDER BY rand()
      LIMIT 3
     ) r JOIN
     recipeproducts rp
     ON rp.recipeID = r.recipeID JOIN
     products p
     ON rp.productID = p.productID;

这假设所有配方都至少有一种产品。

这对我很有帮助。它并不完全有效。它在p.productQuantity处给出一个未知列。