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%'
Related
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'm developing a system that have a Users table and a Books table.
I need to implement a Users search, and a results page showing all Users and all Books belonging to each user.
Currently, I'm using GROUP_CONCAT to get the Books of each User.
Alternatively, using LEFT JOIN brings duplicate results; Then I have to manipulate the data in PHP(is this 'better'?) Off course, I can't use GROUP BY as I need all Books.
I dont have any problems with GROUP_CONCAT, but now I need to filter by Book's and putting 'WHERE books.name like "% name%"' will filter the GROUP_CONCAT result, then showing just the searched book in the filter(while I need all users books)
Wich is the best method to do a search with One-to-Many results in PHP / MySQL ?
i am using moset tree, i am creating search functionality in which
when user enters keyword such as college, then it should search in database with a value of college in tags column. if college matches then that entry should be displayed in result.
My Database View:
First Table: mt_links in which all the listing is saved.
Second Table: mt_cfvalues in which tags were saved and they were normalized
means in first and second table link_id is same. so we need to make relation with this column.
I created this query:
SELECT * FROM #__mt_links link INNER JOIN #__mt_cfvalues val
ON link.link_id = val.link_id AND val.value LIKE '%$keywords%'"
but in this way i am not getting all the results.
Tags Example: in mt_cfvalues table there is one column name as value which are keywords on the basis of which in need to get listing.
Example 1: college, university, institute
Example 2: repair, computer, laptop
if i enter any of keyword mentiond above eg: laptop which is third in listing then even i need to show that relative listing. means product in which laptop tags is used that product should be displayed as a result.
I hope i give clear information. please Help
Thanks
SELECT
*
FROM
#__mt_links link
INNER JOIN #__mt_cfvalues val ON (
link.link_id = val.link_id
)
WHERE
FIND_IN_SET(val.value,'$keywords') != 0
$keywords needs to look like : keyword1,keyword2,keyword3....,keywordN
This question is about selecting data from multiple tables, joins, Doctrine2, ResultSetMapping, DQL and such stuff.
I have 4 tables:
user
contact
contact_phone
call
With relations as shown on the image: http://i.stack.imgur.com/762Jw.png
Every user can have many contacts, each contact can have many phones and each user can have many calls to/from his contacts. Just like in the real world... I've limited the number of fields in each table just for clarity.
So my problem is that I don't know how exactly to map call numbers to contact names when showing a list of calls for a specific user.
If I want to list all calls of user 1 I do:
$callRepository = $this->getDoctrine()->getRepository('MyBundle:Call');
$calls = $callRepository->findAll(array('user' => 1));
But this will give me just the list of all calls for this user and will not associate number (call.number) with names (contact.name).
I can achieve what I want with plain SQL with this query:
SELECT
c.number,
contact.name
FROM
`call` c
JOIN contact_phone cp ON
cp.number = c.number
JOIN contact ON
contact.id = cp.contact_id
WHERE
c.user_id = contact.user_id
AND c.user_id = 1
Please note that I don't want to select all calls (with SQL) and then map numbers to names with another query from the PHP layer because this way I won't be able to search the calls by name for example.
I was thinking that ResultSetMapping could help me in this case but I have no luck putting the SQL query and the ResultSetMapping together.
Please help,
Thanks!
As per my knowledge, you can acheive by using the below methods. Please go to bottom of the page. you can find Joins... try once..
http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/dql-doctrine-query-language.html
How are this two concepts work together ?
I have a scenario
city table
country table
city.country_id is a FK to country.id
Objective
fetch all the cities and display the country name also
My problem
the fetch method will get the cities from the table
if I need the country name I would have to do an extra search for it or an inner join
but by doing so I make extra queries when they are not necessary (display just the a city info for example)
Question
What is the right way to apply the Data Gateway Pattern in this case.
You should use a join if you need h associated country name. Simply add another method like fetchWithCountry.
Another option is to create a view of the city table that includes the CountryName. Then get your DataGateway to select using the view and save using the table. In your domain objects make the CountryName field a dumb read only field. This approach might seem like a dirty hack but it does simplfy things.