显示多连接查询结果
我有一个 SQL 查询,显示检索到的 2 行,但是当我回显字段 name
时,没有检索到任何内容。有人知道我做错了什么吗?
$sql = 'SELECT *
FROM tag_map
LEFT JOIN tags2 ON tags2.tag_id = tag_map.tag_id
LEFT JOIN video ON video.vid_id = tag_map.vid_id
WHERE tag_map.vid_id=?';
$stmt_tags = $conn->prepare($sql);
$result=$stmt_tags->execute(array($vid_id));
echo $tag_count=$stmt_tags->rowCount(); //shows 2
$tags = array();
while ($row = $stmt_tags->fetch(PDO::FETCH_ASSOC)) {
echo $row['name'];
}
编辑: Var_dump 使用建议的查询
SELECT tag_map.*, tags2.name AS tag_name
FROM tag_map
LEFT JOIN tags2 ON tags2.tag_id = tag_map.tag_id
LEFT JOIN video ON video.vid_id = tag_map.vid_id
WHERE tag_map.vid_id=?
array(4) { ["id"]=> string(4) "1234" ["vid_id"]=> string(32) "8364a8e463052e215a5dc174c92a2f18" ["tag_id"]=> string(32) "4c71a73d001dd9c09c7d9d95907bf1fe" ["tag_name"]=> NULL } array(4) { ["id"]=> string(3) "123" ["vid_id"]=> string(32) "8364a8e463052e215a5dc174c92a2f18" ["tag_id"]=> string(32) "57bb5dd83dc84e7115387886e328b04b" ["tag_name"]=> NULL }
I have an SQL query which displays 2 rows retrieved but when I echo the field name
nothing is retrieved. Anyone know what I am doing wrong?
$sql = 'SELECT *
FROM tag_map
LEFT JOIN tags2 ON tags2.tag_id = tag_map.tag_id
LEFT JOIN video ON video.vid_id = tag_map.vid_id
WHERE tag_map.vid_id=?';
$stmt_tags = $conn->prepare($sql);
$result=$stmt_tags->execute(array($vid_id));
echo $tag_count=$stmt_tags->rowCount(); //shows 2
$tags = array();
while ($row = $stmt_tags->fetch(PDO::FETCH_ASSOC)) {
echo $row['name'];
}
EDIT: Var_dump using suggested query
SELECT tag_map.*, tags2.name AS tag_name
FROM tag_map
LEFT JOIN tags2 ON tags2.tag_id = tag_map.tag_id
LEFT JOIN video ON video.vid_id = tag_map.vid_id
WHERE tag_map.vid_id=?
array(4) { ["id"]=> string(4) "1234" ["vid_id"]=> string(32) "8364a8e463052e215a5dc174c92a2f18" ["tag_id"]=> string(32) "4c71a73d001dd9c09c7d9d95907bf1fe" ["tag_name"]=> NULL } array(4) { ["id"]=> string(3) "123" ["vid_id"]=> string(32) "8364a8e463052e215a5dc174c92a2f18" ["tag_id"]=> string(32) "57bb5dd83dc84e7115387886e328b04b" ["tag_name"]=> NULL }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
SELECT
子句应该具体,尤其是在连接其他表时。例如,
请注意我如何使用
tags2.name
的别名来避免列名与tag_map.name
发生冲突。我只是在这里猜测,但我想说您的左连接表之一中有一个
name
列,它覆盖了tag_map.name
。更新
鉴于额外的信息,我不得不说这完全是由于您的左连接所致。
tags2.name
显示 NULL 的记录在tag_map
中根本没有与相关vid_id
相关的字段。如果您只想显示
tag_map
和tags2
之间存在关系的记录,请在此处使用
INNER JOIN
演示 - http://sqlize.com/UL1M80DT0cYou should be specific in your
SELECT
clause, particularly when joining other tables.For example
Notice how I've used an alias for
tags2.name
to avoid a column name conflict withtag_map.name
.I'm only guessing here but I'd say you have a
name
column in one of your left-joined tables that's overridingtag_map.name
.Update
In light of the extra information, I'd have to say it's solely due to your left-joins. The records where
tags2.name
is showing NULL simply do not have related fields intag_map
for thevid_id
in question.If you only want to show records where there is a relation between
tag_map
andtags2
, useINNER JOIN
Demo here - http://sqlize.com/UL1M80DT0c
更明确地说明
SELECT
列表中的列和表:如果您不需要所有表中的所有列,请删除不需要的表。更好的是仅显式命名您需要的列,如下所示:
Be more explicit about the columns and tables in your
SELECT
list:If you don't need all columns from all tables, remove the tables you don't need. Even better is to explicitly name only the columns you need, as in:
您确定有一个名为
name
的列吗?数组键区分大小写,因此如果它是Name
那么您就需要它。另外,尝试
var_dump($row)
看看里面都有什么。Are you sure there's a column called
name
? Array keys are cases sensitive, so if it'sName
then you need that.Also, try a
var_dump($row)
to see what all is there.