Joining tables through relational table - mysql

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

Related

Mysql Query with two seperate join

Does anyone know the solution to this problem ?
There are 3 Tables: orders, order_groups and stores.
I want to list the orders, with the names of the stores where the order was placed, and where the order is going to be delivered.
I keep the from_store_id, and to_store_id in the order_groups table
Listing these orders would be simple, i just left join the order_groups to orders, and select the name, from_shop_id and to_shop_id, but the problem is i want the name of the stores not the id, and the store names are placed in a different table (stores)
Here is what im talking about:
Table orders
id group_id name madeup_id
1 11 johnny cash 1
2 12 billy bob 1
LEFT JOIN order_groups on order_groups.id = orders.group_id
Table order_groups
id from_store_id to_store_id
11 55 56
12 56 55
Table stores
id store_name
55 thisstore
56 thatstore
The result im looking for is:
name from_store to_store
1.johhny cash thisstore, thatstore
2.billy bob thatstore, thisstore
The statement i have yet:
SELECT
orders.name, something as from_store, something as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
somehow join stores on the order_groups.from_store_id = stores.id
WHERE orders.madeup_id = 1
Any idea how to select and join the store names to the query ?
One more question. I actually want to list two kind of orders in one query from different tables too, im on the right track with this structure ?
SELECT a,b FROM a LEFT JOIN b ON b.something=a.something WHERE something
UNION ALL
SELECT a,b FROM c LEFT JOIN c ON c.something=a.something WHERE something
You only need to join 2 times the same table!
SELECT
orders.name, fromStore.store_name as from_store, toStore.store_name as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
left join stores fromStore on the order_groups.from_store_id = fromStore.id
left join stores toStore on the order_groups.to_store_id = toStore.id
WHERE orders.madeup_id = 1

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)

mysql left join duplicates

ive been searching for hours but cant find a solution. its a bit complicated so i'll break it down into a very simple example
i have two tables; people and cars
people:
name_id firstname
1 john
2 tony
3 peter
4 henry
cars:
name_id car_name
1 vw gulf
1 ferrari
2 mustang
4 toyota
as can be seen, they are linked by name_id, and john has 2 cars, tony has 1, peter has 0 and henry has 1.
i simply want to do a single mysql search for who has a (1 or more) car. so the anwser should be john, tony, henry.
the people table is the master table, and im using LEFT JOIN to add the cars. my problem arises from the duplicates. the fact that the table im joining has 2 entries for 1 id in the master.
im playing around with DISTINCT and GROUP BY but i cant seem to get it to work.
any help is much appreciated.
EDIT: adding the query:
$query = "
SELECT profiles.*, invoices.paid, COUNT(*) as num
FROM profiles
LEFT JOIN invoices ON (profiles.id=invoices.profileid)
WHERE (profiles.id LIKE '%$id%')
GROUP BY invoices.profileid
";
try this
select distinct p.name_id, firstname
from people p, cars c
where p.name_id = c.name_id
or use joins
select distinct p.name_id, firstname
from people p
inner join cars c
on p.name_id = c.name_id
If you only want to show people that have a car, then you should use a RIGHT JOIN. This will stop any results from the left table (people) to be returned if they didn't have a match in the cars table.
Group by the persons name to remove duplicates.
SELECT firstname
FROM people P
RIGHT JOIN cars C ON C.name_id = P.name_id
GROUP BY firstname
SELECT DISTINCT firstname
FROM people
JOIN cars ON cars.name_id = people.name_id;
If this doesn't work you might have to show us the full problem.
The way to propose it there's no need for a left join since you need at least a car per person. Left join is implicitely an OUTER join and is intended to return the results with 0 corresponding records in the joinned table.

MySQL create view joining two tables

How can I create a view that merges different columns with a different table? I have three tables for example: users, items and gifts (in this example it's a system that a user can give a gift to another user)
users table has information about users, items table has information about items and gifts table shows which user sent what gift to which user.
What I want is to create a view like following:
user_from | user_to | gift_name | gift_price
sally | john | Teddy Bear | 10
You must join the three tables first. Example
CREATE VIEW GiftsList
AS
SELECT b.name user_from,
c.name user_to,
d.name gift_name,
d.price gift_price
FROM gift a
INNER JOIN users b
ON a.user_from = b.id
INNER JOIN users c
ON a.user_from = c.id
INNER JOIN items d
ON a.item = d.id
You can create a view with two tables like:
CREATE VIEW giftList AS
SELECT users.user_from,users.user_to,gifts.gift_name,gifts.gift_price FROM users,gifts
WHERE users.user_id = gifts.user_id;
The where clause is to make sure the output does not repeat.
I believe were looking for data blending. So basically having google data studio do a JOIN statement on ids from 2 data sets

Search query - Postcode, Company name or Location

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>