Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SQL和PHP从不同的表中搜索数据_Php_Mysql_Sql_Mysqli - Fatal编程技术网

使用SQL和PHP从不同的表中搜索数据

使用SQL和PHP从不同的表中搜索数据,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我有4个数据库表office、facility、course、treatment。office表中的id作为所有其他3个表中的office id 办公室 id name address phoneno city 1 O1 address1 12 city1 2 O2 address2 34 city2 3 O3 address2 45 city3 设施 id office_facility

我有4个数据库表office、facility、course、treatment。office表中的id作为所有其他3个表中的office 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);
?>

如果有人能告诉我如何从这些表中搜索办公室,我将不胜感激。

您可能希望对数据库执行一个复杂的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和布尔逻辑on和/或的值现在由您决定。

您可以使用union

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

您可以使用连接查询。事实上,你还没有弄清楚你需要什么,所以我想给你一个基本的全球例子

"select from table_name as td 
left join anoather_table as anoth on td.common_column = anoth.commoncolun whare td.condition_columh= condition"

基本上你需要什么?@Miya G我想根据设施、课程和课程来寻找办公室treatment@jane字体但这意味着什么?您希望选择办公室并限制结果,以便该办公室必须有特定的设施、课程和治疗?或者别的什么?