views:

81

answers:

3

Hi everyone,

I have two tables, landlords and properties. My properties table has; ID, Address, Postcode, lease and landlordID in it. The problem I face is: If I want to search for all the properties that have Mr.Spina as their landlord I need search the landlords database with the name "spina" to get his ID which is saved in the properties database from which I can extract the properties details.

I thought this would work but it doesn't properly:

> 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%'

I have attached images of the table structures.

Landlords:

only allowed one link

Properties:

http://img5.imageshack.us/img5/7199/propertyn.gif

The result of inserting "spina" into the field should then be: only allowed one link

Here is my extracted code...

>  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();

Many thanks in advance!

A: 

You said:

> SELECT property.ID, property.address, property.postcode, property.lease, firstName, lastName FROM property INNER JOIN landlords ON firstName LIKE '%spina%' OR lastName LIKE '%spina%'

Your join expression needs to say how the two tables are related, for instance

... LEFT JOIN landlords ON landlords.ID == property.landlord_ID ...

And then move your selection into a WHERE clause:

... WHERE firstName LIKE '%spina%' OR lastName LIKE '%spina%'

example:

> 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
Alex Brown
A: 

Seems like you're using INNER JOIN incorrectly; what you want is something like this:

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%'
Marek Karbarz
Many thanks, your post combined with the one above did the tick!
nicky
+1  A: 

In your sample query, you need to join on the landlord 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%'
Willis Blackburn