I am writing a very simple sql query but for some reason I am unable to pull it through. I have mysql table with the following fields; cities, towns and persons. There are several cities, with several towns, which have several people. I want to be able to select a city, say London and be able to see all the towns under london and be able to see everyone living in those towns. What is happening at the moment is that when I query all the tables using their ids, I get a repeat of categories.
I am attaching a picture of what I am trying to achieve
What I am achieveing is
city1 -town1 - person1
city1-town 2-person 1
$query = "SELECT *
FROM cities, Transport, town
WHERE cities.id = town.city
AND town.townID = person.townID
GROUP BY cities.cityName"
I do not want the city to repeat, but at the same time I want a list of everything in that city
Related
I have 2 tables in my DB such as State and Interest.
I am building an app where I need to implement generic search like google.
Means, user can enter combinations like below:
Music in California [Music belongs to Interest and California
belongs to State]
Florida
Music etc.
Now, I want to get best results by applying indexing on both tables State and Interest.
But, before that I need to categorize the query words to identify where should I apply it to get best results.
State table contains states in USA like Florida, Albama, California etc.
and
Interest table contains interests like Music, Concerts etc.
I am new to PHP and sphinx. I need suggestions and no coding answers required for now.
How do the two tables 'join' ? - ie how know there results for Music in California.
At the moment, just mentioning two seperate tables.
Seems you have some other 'table' of actual documents? The items want to actully find. ie you want to find documents about Music in California
... so its THIS table you actully index. Interest and State are just two fields on this index.
Then could search
$query = "keywords #state Claifornia #interest Music";
But actully just searching without fieldnames would work too
$query = "keywords Claifornia Music";
but if California happens in some other fields (like a description) will match too.
First of all I apologise for the title of the question as I am not completely sure what this kind of query is called in mysql.
OK SO I have a large database 700,000 + records containing overseas property / real estate listings.
NOTE : I am using mysql pdo with prepared statments in the actual live site .
I have the following query
$propertyType = 'House';
//// This is generated from a drop down with possible values of House, Apartment & Land
$sql = 'SELECT * FROM PropertyTable WHERE
PropertyType = $propertyTpye'
Now This query works fine when the property is listed in the database as a house, apartment or land. My problem is that The database is populated from several xml feeds from multiple estate agents around the world and these estate agents call things differently.
For example in the UK a house is called a house else where a "house" can be called a "villa", a "town house", "detached house", "chateau", "mansion" etc.
And then for Apartments there can be "Apartment", "flat", "penthouse" etc.
What is the best solution for search the database table. I do not want to put all these options in the drop down menu because its getting to sepecific and will not return enough results.
Any pointers would be greatly appreciated
You would need a table ( say PropertyNames ) that matches each property name variant to its unique property type name. Then you would use a query such as the following:
SELECT distinct a.* from PropertyTable a JOIN PropertyNames b ON b.propertyName ='$propertyName' and a.propertyType = b.nameVariant.
Here, the $propertyName refers to the unambiguous name of the property in the PropertyNames table, which you would use in the drop down selection.
I have made a webpage for a school project where I have a series of selection boxes (some are independent and some are depended on another) to make selections and then apply filter to make a query. It is still in test-mode and working fine for single selections. You can find the webpage here: http://gorevler.awardspace.biz/realdeal03.html
For example, when I select Europe from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter it gives out the result.
But, when I select Europe AND North America from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter, it gives only one result. but there are two records matching this filter in the database. So it does not recognise my multiple selection. Any advice about how I could make it work so that when I make multiple selection on a box and apply filter, it gives all the results including all the selection?
I guess it is something about the PHP coding, and I tried different things but no success yet. Since I am not a programmer, I don't seem to understand where the problem lays. Any advice about the coding would be appreciated.
Is it because when you select two regions your query becomes:
SELECT ... FROM ... WHERE region ='North America' AND region = 'Europe' which means you will never get any results from the database as the region cannot be both? If so you'll need to make it so you use "OR" statements for multiple selected regions.
There are numerous ways of completing this. First of all you need a way of dealing with multiple inputs for your region, rather than just defining the one input in your php code. Basically once you have capture your multiple regiones then your query line is the only line that needs to change. This can become:
$sql = mysql_query("SELECT * from gorevler WHERE region IN ('%$bolge%','%$bolge2%','%bolge3%') AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");
Or
$sql = mysql_query("SELECT * from gorevler WHERE (region = '$bolge' OR region = '$bolge2' OR region = '$bolge3) AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");
EDIT with some pointers on your last question:
It just needs to come from your HTML. So if you select more than one region then it will post these to your PHP page. For example:
<option value="" data-filter-type="" selected="selected" id="europe">Europe</option>
<option value="" data-filter-type="" selected="selected" id="usa">USA</option>
then in your PHP
$bolge=$_POST['europe];
$bolge2=$_POST['usa'];
etc.
That's probably the quick and dirtiest method anyway.
I am creating a mysql database with companies that cover a certain postcode. I want users to be able to type in a complete postcode (zipcode) (eg SW1 1FT) and the query return all company names etc that cover the postcode (SW1)
my first table "members" will contain "ID", "Company_Name", "Phone_Number",and possibly "Postcodes_covered"
Now I understand that listing several postcodes under the Postcodes_covered column is big no no! Can anyone offer any advise or can i just add SW1, SW2, SW3 etc to a single column under postcodes_covered? Taking into account that there are many many post code areas!
Or should I be adding a second table called postcodes which links to the members table? If so how would you go about linking a post code area eh "SW1" to the relevent members and what would the search query look like when someone enters a postcode in the search bar?
Use a separate table with all the postcodes. Then you can use a wildcard to find all the matches:
SELECT *
FROM members m
JOIN postcodes p ON m.id = p.member_id
WHERE p.postcodes_covered LIKE 'SW1%'
I'm using html SELECT to make 3 drop-down menus. One for a list of countries, one for regions and one for cities.
They all have different tables in the database and they are linked to each other with foreign keys.
I want to force the user to first select a country while the other two menus are locked. When he has selected a country he will be able to select a region, and then a city. How do I do this?
Which part are you specifically having problems with?
1) Populate the countries list when the user loads the page, other 2 menus are disabled. (SELECT * FROM Countries;)
2) When the user selects a country, send an AJAX request to the server containing the country name. (SELECT * FROM Regions WHERE id = country_id;)
3) When you receive the AJAX response, populate the regions list and enable it.
4) Repeat steps 2 and 3 for the city name.
You've got several possibilities.
You could write a single select that retrieves all the country-region-city combinations at one time, and use the rows to create Javascript objects representing this data at load time. Then create a Javascript method that updates the dependent rows when a new country or region is selected.
You could write 3 select statements, 1 for countries, 1 for regions for a given country, and 1 for cities for a given region. The countries select would run at page load time, and the other queries could be performed using an Ajax request when country and region are selected.
Try this premade script and start accepting answers.., people might be more willing to help.