显示多连接查询结果

发布于 2024-12-11 20:42:06 字数 1124 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2024-12-18 20:42:06

您的 SELECT 子句应该具体,尤其是在连接其他表时。

例如,

SELECT tag_map.vid_id, tag_map.name, tags2.name AS tag_name

请注意我如何使用 tags2.name 的别名来避免列名与 tag_map.name 发生冲突。

我只是在这里猜测,但我想说您的左连接表之一中有一个 name 列,它覆盖了 tag_map.name

更新

鉴于额外的信息,我不得不说这完全是由于您的左连接所致。 tags2.name 显示 NULL 的记录在 tag_map 中根本没有与相关 vid_id 相关的字段。

如果您只想显示 tag_maptags2 之间存在关系的记录,请

在此处使用 INNER JOIN 演示 - http://sqlize.com/UL1M80DT0c

You should be specific in your SELECT clause, particularly when joining other tables.

For example

SELECT tag_map.vid_id, tag_map.name, tags2.name AS tag_name

Notice how I've used an alias for tags2.name to avoid a column name conflict with tag_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 overriding tag_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 in tag_map for the vid_id in question.

If you only want to show records where there is a relation between tag_map and tags2, use INNER JOIN

Demo here - http://sqlize.com/UL1M80DT0c

丢了幸福的猪 2024-12-18 20:42:06

更明确地说明 SELECT 列表中的列和表:

$sql = "SELECT tag_map.*, tags2.*, video.* 
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=?";

如果您不需要所有表中的所有列,请删除不需要的表。更好的是显式命名您需要的列,如下所示:

SELECT 
  tag_map.name,
  tag_map.id,
  ...
  ...
  video.length
FROM ...etc...

Be more explicit about the columns and tables in your SELECT list:

$sql = "SELECT tag_map.*, tags2.*, video.* 
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=?";

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:

SELECT 
  tag_map.name,
  tag_map.id,
  ...
  ...
  video.length
FROM ...etc...
雪化雨蝶 2024-12-18 20:42:06

您确定有一个名为 name 的列吗?数组键区分大小写,因此如果它是 Name 那么您就需要它。

另外,尝试 var_dump($row) 看看里面都有什么。

Are you sure there's a column called name? Array keys are cases sensitive, so if it's Name then you need that.

Also, try a var_dump($row) to see what all is there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文