我的情况基本上是这样的:我有3个表:itemsforsale,itemsforrent,wantitems。这些表中的每一个都具有相同的列数和相同的名称。我想从这3个表中选择所有数据,其中USERID等于我选择的id并将它们打印为行。它不起作用。这是我的代码:
$sql = "SELECT sale.propertype as propertytype,sale.description as description,sale.price as price,sale.telefone1 as telefone1,sale.telefone2 as telefone2,sale.userid as userid,sale.propertyid as propertyid,sale.area as area,sale.dateposted as dateposted,sale.datebumped as datebumped,sale.images as images,sale.hiddentype as hiddentype,sale.pricetype as pricetype,sale.size as size FROM itemsforsale sale WHERE userid = '1'
UNION SELECT rent.propertype as propertytype,rent.description as description,rent.price as price,rent.telefone1 as telefone1,rent.telefone2 as telefone2,rent.userid as userid,rent.propertyid as propertyid,rent.area as area,rent.dateposted as dateposted,rent.datebumped as datebumped,rent.images as images,rent.hiddentype as hiddentype,rent.pricetype as pricetype,rent.size as size FROM itemsforrent rent WHERE userid = '1'
UNION select wanted.propertype as propertytype,wanted.description as description,wanted.price as price,wanted.telefone1 as telefone1,wanted.telefone2 as telefone2,wanted.userid as userid,wanted.propertyid as propertyid,wanted.area as area,wanted.dateposted as dateposted,wanted.datebumped as datebumped,wanted.images as images,wanted.hiddentype as hiddentype,wanted.pricetype as pricetype,wanted.size as size from itemswanted wanted WHERE userid = '1'";
$result = mysqli_query($mysqli, $sql);
错误: 警告:mysqli_fetch_array()期望参数1为mysqli_result,第290行的blahblah中给出布尔值
我无法让它正常工作。有什么帮助吗?
答案 0 :(得分:1)
您不能将SELECT *
与UNION
一起使用,您必须指定列名称。 Here文档说的是:
第一个SELECT语句中的列名用作 返回结果的列名称。列出的选定列 每个SELECT语句的相应位置应该相同 数据类型。 (例如,第一列选择的第一列 语句应与选择的第一列具有相同的类型 其他陈述。)
此外,要应用Ordering,您需要在UNION查询之外使用ORDER BY
。
要将ORDER BY或LIMIT应用于单个SELECT,请放置该子句 在括起SELECT
的括号内