IBM*_*dig 0 mysql inner-join subquery
我有两个recipes_sa带列的表:
recipes_id recipes_name recipes_chef
---------- ------------ ------------
Run Code Online (Sandbox Code Playgroud)
并chefs_sa带有列:
chefs_id chefs_name
-------- ----------
Run Code Online (Sandbox Code Playgroud)
我想获得有限数量的食谱,其中包含他们的厨师详细信息,使用INNER JOIN和LIMIT
我做了以下功能:
function getLimitJoinData($data, $tbls, $ids, $abr, $type, $limit) {
$dataToSelect = implode($data, ',');
$q = "SELECT $dataToSelect";
$q.= " FROM (SELECT * FROM $tbls[0] LIMIT $limit) $abr";
for ($i=1; $i < count($tbls); $i++) {
$q .= " ".$type." JOIN ". $tbls[$i] ." ON " . $abr.'.recipes_chef' .' = '. $ids[$i-1][0];
}
}
Run Code Online (Sandbox Code Playgroud)
查询是这样的
SELECT chefs_sa.chefs_name,
recipes_sa.recipes_name
FROM (SELECT * FROM recipes_sa LIMIT 8) rec
INNER JOIN chefs_sa ON rec.recipes_chef = chefs_sa.chefs_id
Run Code Online (Sandbox Code Playgroud)
但是当我运行查询时,我收到了以下警告:
警告:PDO::query(): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'recipes_sa.recipes_name' 我不明白为什么
我recipes_name在recipes_sa表中有列,从我读到的数据库首先运行“内部查询”(有限制的查询),然后怎么找不到 recipes_name 列!!
你给recipes_sa AS 取了 别名rec。使用以下内容:
SELECT chefs_sa.chefs_name,
rec.recipes_name
FROM (SELECT * FROM recipes_sa LIMIT 8) rec
INNER JOIN chefs_sa ON rec.recipes_chef = chefs_sa.chefs_id
Run Code Online (Sandbox Code Playgroud)