在一个查询中从两个 mysql 表中选择数据
Posted
技术标签:
【中文标题】在一个查询中从两个 mysql 表中选择数据【英文标题】:Selecting data from two mysql tables in one query 【发布时间】:2010-01-22 17:45:37 【问题描述】:我有两张桌子,房东和财产。我的属性表有; ID,地址,邮政编码,租约和房东ID。我面临的问题是:如果我想搜索以 Mr.Spina 作为房东的所有房产,我需要搜索名为“spina”的房东数据库,以获取保存在房产数据库中的他的 ID,我可以从中获取提取属性详细信息。
我认为这会起作用,但它不正确:
> SELECT property.ID, property.address, property.postcode, property.lease, landlords.firstName, landlords.lastName FROM property INNER JOIN landlords ON landlords.firstName LIKE '%spina%' OR landlords.lastName LIKE '%spina%'
我附上了表格结构的图片。
房东:
只允许一个链接
属性:
http://img5.imageshack.us/img5/7199/propertyn.gif
在字段中插入“spina”的结果应该是: 只允许一个链接
这是我提取的代码...
> if($field=="landlord")
>
> $sql="SELECT property.ID, property.address, property.postcode,
> property.lease, landlords.firstName,
> landlords.lastName FROM ".$do." INNER
> JOIN landlords ON landlords.firstName
> LIKE '%".$q."%' OR landlords.lastName
> LIKE '%".$q."%'";
> else
> $sql="SELECT * FROM ".$do." WHERE " . $field . " LIKE '%" . $q . "%'";
> //end special case $result =
> mysql_query($sql);
> echo "$sql";
> echo "<table border='1'>
> <tr>
> <th>ID</th>
> <th>Address</th>
> <th>Post Code</th>
> <th>Lease</th>
> <th>Landlord</th>
> </tr>";
>
> while($row =
> mysql_fetch_array($result))
>
> echo "<tr>";
> echo "<td>" . $row['ID'] . "</td>";
> echo "<td>" . $row['address'] . "</td>";
> echo "<td>" . $row['postcode'] . "</td>";
> echo "<td>" . $row['lease'] . "</td>";
> echo "<td>" . $row['firstName'] ." ". $row['lastName'] ."</td>";
> echo "</tr>";
> echo "</table>";
>
> mysql_close();
提前非常感谢!
【问题讨论】:
【参考方案1】:在您的示例查询中,您需要加入房东 ID。
SELECT property.ID, property.address, property.postcode, property.lease,
landlords.firstName, landlords.lastName
FROM property INNER JOIN landlords ON landlords.id = property.landlordID
WHERE landlords.firstName LIKE '%spina%' OR landlords.lastName LIKE '%spina%'
【讨论】:
【参考方案2】:您似乎错误地使用了INNER JOIN
;你想要的是这样的:
SELECT
property.ID, property.address, property.postcode, property.lease, landlords.firstName, landlords.lastName
FROM
property
INNER JOIN
landlords
ON
landlords.ID = property.landlord
WHERE
landlords.firstName LIKE '%spina%'
OR
landlords.lastName LIKE '%spina%'
【讨论】:
非常感谢,您的帖子与上面的帖子相结合!【参考方案3】:你说:
> SELECT property.ID, property.address, property.postcode, property.lease, firstName, lastName FROM property INNER JOIN landlords ON firstName LIKE '%spina%' OR lastName LIKE '%spina%'
例如,您的连接表达式需要说明这两个表是如何相关的
... LEFT JOIN landlords ON landlords.ID == property.landlord_ID ...
然后将您的选择移动到WHERE
子句中:
... WHERE firstName LIKE '%spina%' OR lastName LIKE '%spina%'
示例:
> cat > landlords.csv
1,Alex,Spina
2,Spina,Brown
3,Katell,Jentreau
> cat > properties.csv
1,toytown
2,hogwarts
3,mars
4,new york
2,sheffield
> sqlite3
> CREATE TABLE landlords (ID,firstname,lastname);
> .import "landlords.csv" "landlords";
> CREATE TABLE properties (landlord,address);
> .import "properties.csv" "properties";
> SELECT * FROM properties JOIN landlords ON landlord = ID
WHERE (firstname LIKE "Spina" OR lastname LIKE "Spina");
landlord,address,ID,firstname,lastname
1,toytown,1,Alex,Spina
2,hogwarts,2,Spina,Brown
2,sheffield,2,Spina,Brown
【讨论】:
以上是关于在一个查询中从两个 mysql 表中选择数据的主要内容,如果未能解决你的问题,请参考以下文章