Search query - Postcode, Company name or Location - mysql

I am a bit stuck with SQL queries.
User should be able to search Postcode, Company name or Location
I have the following tables:
company table
companyid | name | location
1 Shop One New York
2 Shop Two France
postcode_areas table
postcode | companyid
BB1 1
BB3 1
BB1 2
So if user type in BB1, then it should show the result Shop One, and Shop Two.
If user type the name of company or location - it will just search from company table.

Use a join of both tables and do an OR-search on all fields:
SELECT DISTINCT c.* FROM company c JOIN postcode_areas p USING (companyid)
WHERE c.name = "$QUERY" OR c.location = "$QUERY" OR p.postcode = "$QUERY";
You might use LIKE "%$QUERY%" to get results for partial queries.

Maybe:
select distinct c.*
from company c
join postcode_areas p on p.company_id = c.company_id
where c.name like <input>
or c.location like <input>
or p.postcode like <input>

Related

Joining tables through relational table

I am looking to get a full company list with associated contacts. Even if a company does not have any contacts they should still be included in the results. The Company and People table are linked by a relations value table.
I have created a SQL fiddle for the scheme and would appreciate it if someone could write the query for me.
http://sqlfiddle.com/#!2/34a0a
I would see the result look like:
Company Name 1 | Person Number 1 <br>
Company Name 1 | Person Number 2 <br>
Company Name 2 | Person Number 3 <br>
Company Name 2 | Person Number 4 <br>
Company Name 2 | Person Number 5 <br>
Company Name 2 | Person Number 6 <br>
Company Name 3 | NULL <br>
You can achieve this result with two joins - one to your relations table, then another to your persons table. If you use a left join, the link will not be forced, so the company without a person/relation will still be returned but with null as the person result:
SELECT
C.*,
P.*
FROM
companies C
LEFT JOIN
relations R
ON (R.parentmodule = 'companies' AND R.parentrecordid = C.id)
LEFT JOIN
people P
ON (R.childmodule = 'people' AND R.childrecordid = P.id)
Output
The following query gets the desired output. Since an inner join is used between two tables, it should be slightly faster.
SELECT
c.companyname,
p_r.firstname,
p_r.lastname
FROM companies c
LEFT JOIN
(SELECT
r.parentrecordid,
p.firstname,
p.lastname
FROM relations r
INNER JOIN people p ON p.id = r.childrecordid AND r.childmodule = 'people'
) p_r
ON p_r.parentrecordid = c.id;
SQL Fiddle demo

Free search on multiple tables using JOIN

I have a DB of Students who have a fullname, a location, and a list of schools they frequented.
"student" table
id | fullname | location
------------------------
"location" table
id | zipcode | city
-------------------
"school" table
id | name
---------
"student_school" table (which holds two foreign keys on school and user to create for each user a list of schools)
id | id_student | id_school
---------------------------
I want to perform a search through the students comparing the search term with student.fullname, location.zipcode, location.city, school.name and return all the students matching one (or more) of these conditions.
Note that student can have a null location, so we need an extern join.
Here is example of matching based on regular expressions:
SELECT distinct s.id, s.fullname
FROM student_school ss
JOIN student s ON s.id = ss.id_student
LEFT JOIN LOCATION l ON s.LOCATION = l.id
JOIN school sc ON ss.id_school = sc.id
WHERE (s.fullname RLIKE 'Sasha.*') or
(ifnull(l.zipcode RLIKE '100.*', 0)) or
(ifnull(l.city RLIKE 'New.*', 0)) or
(sc.name RLIKE '.*2')
And here is complete SQL fiddle
So the general idea is to join all student data, filter rows matching at least one criterion and use distinct to group data and avoid duplicating of results.
Update:
To include students with no school records you may use following FROM phrase:
student_school ss
RIGHT JOIN student s ON s.id = ss.id_student
LEFT JOIN LOCATION l ON s.LOCATION = l.id
LEFT JOIN school sc ON ss.id_school = sc.id

MySQL, Show value of column if match exist else leave as null

I'm sure this has been asked before but can't find the answer.
I have 3 tables OWNER, CAR, HOUSE
OWNER has 2 columns id and name
CAR has 3 columns id, ownerId and cartype
HOUSE has 4 columns id, ownerId, address, country
I want to write a SQL query that gets the owners name, cartypes, and addresses that are in Sweden
Here comes the tricky part. I want all the owners names and cartypes in the result table even if they don't own a house in Sweden. Can I get all that in 1 query or do I need to use 2? How would that query look?
You should be able to accomplish this with a simple left join:
SELECT O.name, C.cartype, H.address, H.country
FROM OWNER AS O
JOIN CAR AS C ON O.id = C.ownerid
LEFT JOIN HOUSE AS H ON O.id = H.ownerid AND Ucase(H.country) = "SWEDEN"
This will always give you a list of owners and their car types, in addition, it will give you a list of those that also happen to have a house address in sweden.
First you need to join the table then add new column in query by using CASE to check
SELECT o.* , c.* ,h.*,
(CASE WHEN h.county ='sweden' THEN h.county ELSE NULL END) AS HasCountry
FROM OWNER o
JOIN CAR c ON (c.ownerId =o.id)
JOIN HOUSE h ON (h.ownerId =o.id)

Select id from one table and its value from another table to search

I have two tables named company and customers.
In company table there are 3 fields ID, Company, and Type as:
ID Company Type
1 ABC Running
2 XYZ Current
Now again in customers table I submitted the value of company and customers, but here the value of company is submitted as an ID as:
Company Customer Name
1 monty
2 sandeep
now I want to search the company name in customer table but when i put company name in search box its showing nothing because the value of company name is in ID form in customer tabe.how can i achieve it.
Here is my query for search:
$sql = "Select * from customers where name like '%$term%' or location like '%$term%' or company like '%$term%'";
By JOINing the two tables:
Select *
from customers AS cust
INNER JOIN companies AS comp ON cust.Company = comp.Id
where comp.location like '%$term%'
or comp.company like '%$term%'
try this
SELECT co.*, cu.* FROM company as co, customers as cu where co.company_id = cu.company_id and co.company_name = '%$term%';

mysql query help to fetch data by joining multiple tables

I have following tables:
base:
id | domain
extended:
id | domain | country_code
countries:
id | country_code | country_name
banned_domains:
id | domain
I will have several thousands (more than 500K) of domains in banned_domains. Now I need to fetch data "domain, country_code and country_name" which do not exist on banned_domains list. I am not so good at the MySQL JOINS, can anyone guide me for the proper query.
SELECT b.domain, ex.country_code, c.country_name FROM base b
INNER JOIN extended ex ON b.domain=ex.domain
INNER JOIN countries c ON ex.country_code=c.country_code
WHERE b.domain NOT IN (SELECT domain FROM banned_domains);
You can use this query .
select b.domain , e.country_code , c.country_name from base b join extended e on b.domain = e.domain join countries c on e.country_code = c.country_code and b.domain not in (select domain from banned_domains);
Try this link http://sqlfiddle.com/#!2/f78ea/1 .