无法在php mysql中使用union
嗨,朋友们,我正在尝试将一个表中的一列与另一个表中的一列合并 但是我犯了一个错误。 这是代码无法在php mysql中使用union,php,mysql,concatenation,union,Php,Mysql,Concatenation,Union,嗨,朋友们,我正在尝试将一个表中的一列与另一个表中的一列合并 但是我犯了一个错误。 这是代码 table names are table1 and table2 表1有列名,表2有列名 table1 were you when table2 how yes no 我的php和mysql代码如下 $query="SELECT name FROM table1 UNION ALL select title from table2"; $select_playlist=mysql
table names are table1 and table2
表1有列名,表2有列名
table1
were
you
when
table2
how
yes
no
我的php和mysql代码如下
$query="SELECT name FROM table1 UNION ALL select title from table2";
$select_playlist=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($select_playlist))
{
$title=$row['title'];
$name=$row['name'];
echo $title.$name;
}
其显示错误未定义索引:name
预期产量为
were how
you yes
when no
如果我删除名称并仅打印标题,则打印为
were
you
when
how
yes
no
使用这些表无法获得所需的输出 您需要的是联接:联接将表B中的列追加到表a中。 并集将表B中的行追加到表A 要使用联接,数据库需要知道哪些行属于一起,因为表中的行基本上没有隐含的顺序 表1:
id name
1 were
2 you
3 when
表2:
id title
1 how
2 yes
3 no
然后你可以这样做:
SELECT * FROM table1 JOIN table2 USING(`id`)
结果如下所示:
id name title
1 where how
2 you yes
3 when no
(DBA:请不要因为草率的术语而责怪我——在这里谈论元组和集合会让用户感到困惑)union将两列合并为一列。因此,您只能在$row['title']中获取数据。如果要使用$row['name']。您应该编写两个不同的查询。但是看一下您的编码,您需要连接两个表。可能会给您想要的输出的操作是连接,而不是联合。也就是说,为了进行连接,您需要能够关联名称和标题列,以便以有意义的方式将它们匹配起来。也许你应该发布完整的表定义。你需要一个连接,而不是一个联合。对于该联接,您需要告诉数据库在一个条件下联接,例如两个表中具有相同值的列_fetch@verhie我没有公共领域