我有4个数据库表(办公室,设施,课程,治疗),办公室表中的id在所有其他3个表中充当办公室id。
办公室
id name address phoneno city
1 O1 address1 12 city1
2 O2 address2 34 city2
3 O3 address2 45 city3设施
id office_facility office_id
1 F1 1
2 F2 1
3 F3 2课程
id office_course office_id
1 C1 1
2 C2 2
3 C3 3治疗
id office_treatment office_id
1 T1 1
2 T2 2
3 T3 2我试着根据设施、课程和待遇来寻找办公室。当只有一个表,并且搜索条件是同一个表的一部分时,我使用的搜索代码是有效的,但是这种情况不同。
搜索的代码是
<?php
$con=mysqli_connect("localhost","root","","db");// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$city = mysqli_real_escape_string($con, $_POST['city']);
$sql1 = "SELECT * FROM table WHERE city LIKE '%$city%'";
$result = mysqli_query($con, $sql1);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)) {
echo "Office name: " . $row["office_name"]. " - Location: " . $row["office_address"]. " " . $row["office_city"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($con);
?>如果有人能告诉我如何从这些表格中搜索办公室,我将不胜感激
发布于 2014-12-01 19:19:38
您可能希望对数据库执行复杂的SQL查询,将您拥有的所有条件连接起来:
SELECT address FROM office
JOIN facility ON (facility.office_id = office.id)
JOIN course ON (course.office_id = office.id)
JOIN treatment ON (treatment.office_id = office.id)
WHERE city LIKE %...%
AND office_facility LIKE %...%
AND office_course LIKE %...%
AND office_treatment LIKE %...%LIKE的值以及AND/OR上的布尔逻辑现在由您决定。
发布于 2014-12-01 19:21:19
为此,您可以使用联合
select A.ID,(SELECT B.Name from office B where A.ID=B.ID) as Name from (
select id from office where <Your Office Table filter Criteria>
UNION
select office_id from facility where <Your facility Table filter Criteria>
UNION
select office_id from Course where <Your Course Table filter Criteria>
UNION
select office_id from Treatment where <Your Treatment Table filter Criteria>
)A发布于 2014-12-01 19:22:26
您可以使用join查询。实际上,您还没有说明您需要什么,所以我尝试给您提供相同的基本(全局)示例
"select from table_name as td
left join anoather_table as anoth on td.common_column = anoth.commoncolun whare td.condition_columh= condition"https://stackoverflow.com/questions/27227151
复制相似问题