在不知道您确切的表结构的情况下,我会提出类似的建议:
SELECT
stores.name AS name,
addresses.street AS street,
addresses.city AS city,
'store' AS type,
GROUP_CONCAT(categories.categoryname) AS category
FROM
stores
INNER JOIN
addresses ON addresses.addressid = stores.storeid
INNER JOIN
categories AS categories.storeid = stores.storeid
WHERE
stores.name LIKE "ac%"
GROUP BY
stores.storeid
UNION
SELECT
brands.name AS name,
'-' AS street,
'-' AS city,
'brand' AS type,
GROUP_CONCAT(brandcategories.categoryname) AS category
FROM
brands
INNER JOIN
brandcategories AS brandcategories.brandid = brands.brandid
WHERE
brands.name LIKE "ac%"
GROUP BY
brands.brandsid
UNION
SELECT
malls.name AS name,
addresses.street AS street,
addresses.city AS city,
'mall' AS type,
'-' AS category
FROM
malls
INNER JOIN
addresses ON addresses.addressid = malls.mallid
WHERE
malls.name LIKE "ac%"
GROUP BY
malls.mallid
SELECT...
为每个表做一个并联合它们。每个 SELECT 都有一个 column type
,其中 type 可以是 store、brand 或 mall。所以你可以稍后在你的 PHP 代码中区分它。
由于 UNION 中的每个 SELECT 都需要相同的列,因此例如品牌的 SELECT 也会返回一个地址,但它是空的。
GROUP_CONCAT(categories.categoryname)
在此示例中,每个商店/品牌都可以有多个类别,这些类别会在逗号分隔的字段 ( )中一次返回。