简体   繁体   English

在一个查询中将2条选择语句组合在一起

[英]Combining 2 select statements together in one query

I need to combine 2 select statements together but i get an error: 我需要将2条选择语句组合在一起,但出现错误:

Notice: Trying to get property of non-object 注意:试图获取非对象的属性

My code currently: 我的代码当前:

$sql = "(SELECT id, sijainti FROM sijainti
  UNION ALL
SELECT numero FROM tiedot)";

My code works perfectly otherwise if i try to do this only with one select statement. 如果我仅尝试使用一个select语句执行此操作,则我的代码会完美运行。 Any suggestions for making this work? 对这项工作有什么建议吗?

If you use UNION then number of fetched column must be equal, also name of columns must be same, so modify your query like, 如果您使用UNION,则提取的列数必须相等,列名也必须相同,因此请修改查询,

$sql = "(SELECT id AS id, sijainti AS name FROM sijainti UNION ALL SELECT 0 as id, numero AS name FROM tiedot)"; $ sql =“(SELECT id AS id,sijainti AS name FROM sijainti UNION ALL SELECT 0 as id,numero AS name FROM tieot)”;

may be it will help you, 可能会帮助您,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM