Jump to content

Cannot figure OR search right


Voodoo Jai

Recommended Posts

Hi all

I am wanting to search two fields for a LOCATION ("Leeds")  that hold the name of a location. But only get an AND search result.

 

Sample data

ID  Field 1          Field2

1    Leeds          Armley

2    Leeds          Leeds

3    Crossgates    Leeds

 

the search only returns ID 2 not all of them, I need to find all the fields that contain "Leeds".

 

Here is the statement I am using

 

SELECT t.`TakeawayID`, t.`Takeaway_Name`, t.`Line1`, `Location`, C.`City`, t.`Post_Code`
FROM takeaway t, location l, City C
WHERE (Location = "Leeds" OR t.City = "Leeds") AND t.`LocationID`= l.`Town_CityID` AND t.`City`= C.`CityID`
ORDER BY `Location`;

Link to comment
https://forums.phpfreaks.com/topic/122849-cannot-figure-or-search-right/
Share on other sites

First table structure

Takeaway table

+-----------------+---------------------+------+-----+-------------------+----------------+
| Field                 | Type                       | Null   | Key | Default                 | Extra               |
+-----------------+---------------------+------+-----+-------------------+----------------+
| TakeawayID       | int(10) unsigned       | NO    | PRI  | NULL                    | auto_increment |
| Takeaway_Name | varchar(100)            | NO   |        | Takeaway name     |                |
| Line1                | varchar(50)              | NO   |        | Line 1                   |                |
| Line2                | varchar(50)              | YES  |        | NULL                   |                |
| LocationID         | int(10) unsigned        | NO   | MUL | 0                        |                |
| City                  | int(10) unsigned        | NO   | MUL | 0                        |                |
+-----------------+---------------------+------+-----+-------------------+----------------+

Second table

Location table

+-------------------+------------------+------+-----+-------------------+----------------+
| Field                    | Type             | Null | Key | Default           | Extra          |
+-------------------+------------------+------+-----+-------------------+----------------+
| Town_CityID         | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| Location               | varchar(45)      | NO   | UNI |                   |                |
| CountyID              | int(10) unsigned | YES  | MUL | NULL              |                |
| ts                       | timestamp        | NO   |     | CURRENT_TIMESTAMP |                |
+-------------------+------------------+------+-----+-------------------+----------------+

 

Third table

City table

+--------+------------------+------+-----+---------------------+----------------+
| Field      | Type                 | Null    | Key | Default                    | Extra          |
+--------+------------------+------+-----+---------------------+----------------+
| CityID   | int(10) unsigned   | NO    | PRI  | NULL                       | auto_increment |
| City      | varchar(45)         | NO    | UNI |                                |                |
| ts         | timestamp          | NO     |       | 0000-00-00 00:00:00 |                |
+--------+------------------+------+-----+---------------------+----------------+

 

I am trying to search for a location that could be in either the

 

takeaway table

locationID or city field.

 

using the details of the location to search for from the

city table and Location table.

I have missed out some of the field in the takeaway table for clarity.

I am serching one table but I want to find all rows with either a specific value in:

 

FIELD-A or FIELD-B

or in:

FIELD-A and FIELD-B

 

So Sample data that searches for "LEEDS"

 

ID  Field 1          Field2

1    Leeds          Armley

2    Leeds          Leeds

3    Crossgates    Leeds

 

Much appreciate your time to help me

 

VoodooJai

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.