从多个表字段返回结果

时间:2017-01-13 09:23:48

标签: php sql

我从查询中得到了返回结果,但我希望它返回多个结果,这是当前查询:

$qry = "SELECT * FROM properties WHERE isLeased = 0 AND featured = 1 ORDER BY RAND() LIMIT 9";
$res = mysqli_query($mysqli, $qry) or die('-1' . mysqli_error());

我希望它能够返回这些结果,以及结果:

$qry = "SELECT * FROM properties WHERE isLeased = 0 AND propertyType = 'For Sale' ORDER BY RAND() LIMIT 3";
$res = mysqli_query($mysqli, $qry) or die('-1' . mysqli_error());

但是,如果我只是在第一部分下面添加第二部分代码,它只会返回第一部分的结果。

我怎么能告诉它根据几张表返回结果?

1 个答案:

答案 0 :(得分:1)

您可以使用UNION查询:

SELECT t1.*
FROM
(
    SELECT *
    FROM properties
    WHERE isLeased = 0 AND featured = 1
    ORDER BY RAND()
    LIMIT 9
) t1
UNION ALL
SELECT t2.*
FROM
(
    SELECT *
    FROM properties
    WHERE isLeased = 0 AND propertyType = 'For Sale'
    ORDER BY RAND()
    LIMIT 3
) t2