How to join mysql query with multiple tables - mysql

I have these tables
AssigenmentList --linksto ---School,AgeGroup
Users will have birthday attached to it
AgeGroup in turn linked to many ages like AgeGroup 3-4 is linked to one to many with 3,4 in numerical format
Now i want all the Assignment list which are linked to particular SCHOOL belong to same age as the age of Child

As a general rule:
select a.*, b.*, c.* from
A a inner join B b on a.idB = b.id
inner join C c on b.idC = c.id
You use inner join if a.idB must have match to add the row to the resultset. Left outer join if the mere presence of a.idB (left side) is enough to project the row.
The trick is to navigate from the starting table to the last joining the columns that ties them.

Related

How to select from multiple tables when some of the tables are empty in MySQL

I need to fetch data from 5 tables(all columns of each table) all have FK, which is PK of single table.
But some of the tables may have record may be empty.If data is present on the respective column/table it should return otherwise null/default value
There is one to many and one to one relations on the child tables with parent table.
I have tried so far
- UNION which has concern of same number of columns
- CROSS JOIN not returning any data
- SELECT ALL_COLUMN FROM ALL_TABLE WHERE TABLE.FK=ID Not returning any data
- LEFT JOIN working for 2 tables but not more than that
SELECT A.GENDER, B.BLOCKED_USER FROM t_macroworld_registration AS A
LEFT JOIN t_macroworld_blacklist AS B ON 1=1 WHERE A.ID=15
What are the possible ways I can implement this in a view in MySQL.
Outer join operations are the normative pattern...
SELECT ...
FROM a
LEFT JOIN b ON b.a_id = a.id
LEFT JOIN c ON c.a_id = a.id
LEFT JOIN d ON d.a_id = a.id
WHERE a.id = 15
It's important for the predicates on the outer joined tables to be in the ON clause and not the WHERE clause. If there's any predicate in the WHERE clause requires that a value from one of the outer joined tables be non-NULL, that will negate the "outerness" of the join, making it into an inner join.
The "big rock" problem with this the result when there are more than one matching rows in b, c and d. If there's five rows from b that match, and three rows from c that match, and two rows from b that match, it's going to look like a lot of duplicates. (5x3x2 = 30 rows to be returned, with a lot of duplicated data on those rows.)
Finally I have solved It,
I broke the whole thing into many select query based on FK from each table, so number of additional row returns and mapping has become easy.
Who ever is getting this kind of problem, if it is possible then break it into many select query instead of one.
SELECT
w.id,w.name,a.address,i.name,i.quantity
FROM
warehouse w
LEFT JOIN address AS a ON a.warehouse_id = w.id
LEFT JOIN item AS i ON i.warehouse_id = w.id
LEFT JOIN order AS o ON o.item_id = i.id
WHERE
w.id = 1
GROUP BY 1,2,3,4;
Will give you an overview of your stock and orders for your warehouses. This will also duplicate some results.
Assuming 2 warehouses, 1 address for each, 3 items per warehouse, 2 orders by item = 2 * 3 * 2 = 12 lines
I recommend adding your LEFT JOIN stage by stage and visualizing the result for each stage. You'll quickly understand why lines are multiplying.
Note the usage of foreign keys and ids in the tables to link the tables.
Good luck

MySQL getting data from 3 tables which are connected by mapping tables

I have the following database example:
The example is pretty much self-explanatory: There are lessons held by teachers at defined time periods (time_start, time_end) each time period -> lesson connection has its own max_students number.
I know want to list all lessons with all information of the 3 tables (and the max_students). I would do it like that (I heard, that joining table like that is the fastest way):
SELECT * FROM lesson, teacher, time, teacher_has_lesson, time_has_lesson
WHERE lesson.lesson_id = teacher_has_lesson.lesson_lesson_id
AND teacher.teacher_id = teacher_has_lesson.teacher_teacher_id
AND lesson.lesson_id = time_has_lesson.lesson_lesson_id
AND time.time_id = time_has_lesson.time_time_id
1.) Is this a good solution if you just want to join 3 tables or are there better ones?
2.) This SQL call will get me only lessons, that have a teacher and a time. I also want to display lessons, that are in the database, but dont have a teacher or time yet. How can I do that?
There's an alternative way of writing this using join syntax. What you have is equivalent to an inner join, where you only see rows where there are matches:
select
*
from
lesson l
inner join
teacher_has_lesson tl
on l.lession_id = tl.lesson_lesson_id
inner join
teacher t
on tl.teacher_teacher_id = t.teacher_d
inner join
time_has_lesson tml
on l.lesson_id = tml.lesson_lesson_id
inner join
time tm
on tml.time_time_id = tm.time_ud
There's another type of join called outer join, where all the rows from one table are shown, and null values supplied if there are no matching values in the other table. It comes in two or three variants. left outer join shows all rows from the first table. right outer join shows all rows from the second table. full outer join shows all rows from both tables. So, for your second query you could use:
select
*
from
lesson l
left outer join
teacher_has_lesson tl
on l.lession_id = tl.lesson_lesson_id
left outer join
teacher t
on tl.teacher_teacher_id = t.teacher_d
left outer join
time_has_lesson tml
on l.lesson_id = tml.lesson_lesson_id
left outer join
time tm
on tml.time_time_id = tm.time_ud

MYSQL Use OR in Left Join condition?

I'm not entirely sure that what I'm trying to do is possible. Can you use an OR in the condition of a left join? I start from my users table and then it can either go from week_meal to meal (adding a meal they do not own to their weekly meal plan) or straight to meal (a meal they own). That part appears to be working, but when I include mta.meal_to_add_id in the select, it incorrectly pulls in meals that do NOT meet the criteria in the LEFT JOIN to meal_to_add.
Fiddle with structure: http://sqlfiddle.com/#!2/7bd9c4
SELECT DISTINCT m.*, o.username as owner,i.*, mta.meal_to_add_id, follow_id
FROM webusers wu
LEFT JOIN week_meal wm ON wu.id=wm.user_id
LEFT JOIN meal m ON (wu.id=m.user_id OR wm.meal_id=m.meal_id)
LEFT JOIN webusers o ON m.user_id=o.id
LEFT JOIN meal_to_add mta ON
((wm.user_id = mta.user_id AND wm.meal_id=mta.meal_id)
OR (m.user_id=mta.user_id AND m.meal_id=mta.meal_id))
JOIN ingredient i ON m.meal_id = i.meal_id
LEFT JOIN follow f ON
m.user_id!=wu.id AND
m.user_id=f.followed_webuser_id
AND wu.id=f.followee_webuser_id
WHERE wu.id=5 AND m.meal_id in (138)
ORDER BY m.meal, i.ingredient_id
OUTPUT: It should be just like this only including the field for mta.meal_to_add_id, which in this case should be NULL for all rows (18)
Sample Results
To answer the first part of your question: Yes, you can use an OR clause in a LEFT JOIN.
As for the second part, in plain words, this is what the query seems to say:
Join 'week meals' to users on the user id. Join meals to users on that same user id OR join meals to users on the meal id. Assume now that we have some matching meal/user combinations, where some meal rows are matched on user, and others are matched on the meal id.
Next, join webusers to meals again. Now we have meal rows possibly matching two sets of users. So when mta tries to match meals, it is matching two possible sets of meal/user combinations.
My practice in cases like this is to break up the query into two queries and put the intermediate results in a temp table (using MEMORY engine), then select from that.

How to create the inner query wtih 5 different tables mysql

Having 5 tables
Table a_dates = id,
Table b_types = id, a_date_id, c_type_id,
Table c_types = id, name,
Table d_profiles = id, name, profile_type
Table e_ps = id, a_date_id, d_profile_id
From a_dates Need to get b_types,...then from b_types needs c_types name,... Then compare c_types name with d_profiles name and get d_profiles id.... if equals then create a records in e_ps with a_date_id, d_profile_id.
Could any one please help me in getting the query from inner join.
I tried like, it is incomplete query
INSERT INTO e_ps(id,a_date_id,a_date_type,d_profile_id,c_id)
SELECT '',a.id,'A',dp.id,'67' FROM d_profiles dp
INNER JOIN a_dates a ON {HERE I NEED NAME MATCHING WITH c_types name} = dp.name and dp.profile_type = 'A'
INNER JOIN a_dates ON a.id = a_dates.id
LEFT OUTER JOIN e_ps eps ON eps.a_date_type = 'A' AND eps.a_date_id = a_dates.id
WHERE eps.a_date_id IS NULL
This seems to be a relatively simple JOIN:-
INSERT INTO e_ps(id, a_date_id, d_profile_id)
SELECT NULL, a_dates.id, d_profiles.id
FROM a_dates
INNER JOIN b_types ON a_dates.id = b_types.a_date_id
INNER JOIN c_types ON b.c_type_id = c.id
INNER JOIN d_profiles ON c_types.name = d_profiles.name
With joins there are several types, and I suspect you are getting confused. Briefly:-
With an INNER JOIN it looks for a match that is on BOTH tables. If no
match the no record is returned.
With a LEFT OUTER JOIN it takes a record from the table on the left
and looks for a match on the table on the right. If a match great,
but if not then it still brings a row back but the columns from the
table on the right just have values of NULL.
A RIGHT OUTER JOIN is very much the same, just with the tables
reversed (most people including me avoid using this as it has no
advantages most of the time but just makes things confusing).
With a FULL OUTER JOIN it gets the records from both side, whether
they match or not. If they match then the columns from both are
returned, if not matched then the columns from one are returned. Not
that MySQL does not support a FULL OUTER JOIN (although there are
ways to emulate it).
A CROSS JOIN joins every combination of 2 tables. These are used when
there is no common column to match on but you want all combinations.
For example if you wanted a table of all employees and all days of
the week for each employee you would cross join a table of days of
the week against a table of employees (then for useful data you might
LEFT OUTER JOIN a table of holidays to the result).

Need to use criteria from multiple tables to acquire data from multiple tables

I'm having a bit of a hiccup regarding a particular SQL query. I need to join data from two tables, while also limiting the data (but not necessarily grabbing it) by means of a third table. The tables are as follows:
MEMBERS(member_id,first_name,last_name)
MEMBERS_GROUPS(member_id,group_id)
CHARGES(charge_id,member_id,charge_amount,status)
I need to find all charges for all members of a specific group but I also want to grab the first and last name from the MEMBERS table. The query I've come up with thus far is:
select c.*, m.first_name, m.last_name
FROM charges c
LEFT JOIN member m
ON c.member_id=m.member_id
INNER JOIN members_groups mg
ON mg.group_id=1
i've also tried:
SELECT c.*, m.first_name, m.last_name
FROM charges c, members_groups mg, member m
WHERE c.member_id=mg.member_id
AND mg.group_id = 1
AND c.status='Valid'
AND c.member_id = m.member_id
…but neither returns the data I need. I'm sure I'm overthinking this, but I can't for the life of me get the correct values. I keep getting what appears to be the Cartesian product -- regardless, it's clearly returning too many rows and bad data.
Perhaps what you need is to also restrict the INNER JOIN on members_groups to
those rows with mg.member_id = m.member_id:
SELECT c.*, m.first_name, m.last_name
FROM charges c
LEFT JOIN member m
ON c.member_id=m.member_id
INNER JOIN members_groups mg
ON mg.group_id=1
AND mg.member_id = m.member_id