每次运行这3个查询以从mysql获取数据时,我都会有3个mysql查询。我怎样才能使这3个查询成为单查询。 Belw,我要添加查询。
SELECT b.name as projectname,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON
bu.project_id=b.id
AND b.is_deleted=0
WHERE project_id=?
AND bpu.configuration=?
AND bu.towername=?
AND bu.is_deleted=0
ORDER BY bu.configuration,
bu.tower_name
SELECT b.name as project_name,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON
bu.project_id=b.id
AND b.is_deleted=0
WHERE project_id=?
AND bu.configuration=?
AND bu.is_deleted=0
ORDER BY bu.configuration
SELECT b.name as project_name,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON
bu.project_id=b.id
AND b.is_deleted=0
WHERE project_id=?
AND bu.is_deleted=0
ORDER BY bu.configuration
答案 0 :(得分:1)
您可以使用union all
SELECT b.name as projectname,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON
bu.project_id=b.id
AND b.is_deleted=0
WHERE project_id=?
AND bpu.configuration=?
AND bu.towername=?
AND bu.is_deleted=0
ORDER BY bu.configuration,
bu.tower_name
UNION ALL
SELECT b.name as project_name,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON
bu.project_id=b.id
AND b.is_deleted=0
WHERE project_id=?
AND bu.configuration=?
AND bu.is_deleted=0
ORDER BY bu.configuration
UNION ALL
SELECT b.name as project_name,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON
bu.project_id=b.id
AND b.is_deleted=0
WHERE project_id=?
AND bu.is_deleted=0
ORDER BY bu.configuration
答案 1 :(得分:1)
这个怎么样?。
SELECT b.NAME AS projectname,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON bu.project_id = b.id AND b.is_deleted = 0
WHERE project_id=? AND bu.is_deleted=0
ORDER BY bu.configuration, bu.tower_name
或者这个...
SELECT b.NAME AS projectname,
bu.configuration,
bu.floorpla,
bu.sqft,
bu.project_id
FROM projects b
JOIN project_units bu ON bu.project_id = b.id AND b.is_deleted = 0
WHERE bu.is_deleted = 0
AND ( ( project_id = ? AND bu.configuration = ? AND bu.towername = ? )
OR ( project_id = ? AND bu.configuration = ? )
OR ( project_id = ? ) )
ORDER BY bu.configuration, bu.tower_name
根据需要选择查询。