2017-02-21 112 views
-1

我的情况基本上是这样的:我有3个表:itemsforsale,itemsforrent,wanteditems。这些表中的每一个都具有相同的列数和相同的名称。我想从这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,布尔在blahblah给出线290

我不能让它正常工作。任何帮助?

回答

1

您不能使用SELECT *UNION,您必须指定列名称。 Here的文件说什么:

The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)

此外,申请订购,您需要使用ORDER BY外UNION查询。

To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT

+0

我想选择表中的所有内容。这是如何完成的? – Ali

+0

您可以为所有'SELECT'语句指定逗号分隔的列列表。 –

+0

所以我必须选择所有列而不是*? – Ali

相关问题