How do I delete LEFT JOIN rows that are NULL in mySQL? - mysql

I am doing a LEFT JOIN like this:
SELECT mt_order_delivery_address.id,
mt_order.order_id,
mt_order_delivery_address.client_id
FROM mt_order_delivery_address
LEFT JOIN mt_order ON mt_order.order_id = mt_order_delivery_address.order_id
which then gives me a list like:
id order_id client_id
----------------------------
1032 NULL 382
1028 NULL 282
1020 784344 920
1002 232496 490
I want to get rid of the rows in mt_order_delivery_address where there is no corresponding order_id in mt_order. How would I do this? I can't seem to wrap my head around it.
UPDATE:
I tried this but doesn't seem to work:
DELETE mt_order_delivery_address.id
FROM mt_order_delivery_address
LEFT JOIN mt_order ON mt_order.order_id = mt_order_delivery_address.order_id
WHERE mt_order.order_id IS NULL

If by "delete" you mean "don't show them in this query result" you can use an INNER JOIN instead of a LEFT JOIN. It's worth learning the different types of joins and how to use them.
If by "delete" you mean "DELETE the rows in my_order_delivery_address that have no matching my_order", you can do a multi-table DELETE:
DELETE a FROM mt_order_delivery_address AS a
LEFT JOIN mt_order AS o USING (order_id)
WHERE o.order_id IS NULL;

Related

find records in a table which is not associated in to other records in different table

I have 3 tables :
list_routes
pk_route_id route_code route_name route_description
3 J1 IND IND-DPS
4 J4 ADT ADT_DPS
tbl_stu_route
pk_stu_route_id fk_stu_cls_id fk_route_id
2 13 3
tbl_stu_class
pk_stu_cls_id fk_stu_id
13 56
Now what I want to achieve is write query in MYSQL that it will fetch me the records from list_routes which is not associated or attached in tbl_stu_class so for e.g in the above scenario it should give me the output :
pk_route_id route_code route_name route_description
4 J4 ADT ADT_DPS
I wrote the below query using left outer join as
SELECT
a.pk_route_id,
a.route_code,
a.route_name,
a.route_description
FROM
list_routes a
left outer join tbl_stu_route b on a.pk_route_id=b.fk_route_id
left outer join tbl_stu_class c on b.fk_stu_cls_id=c.pk_stu_cls_id
where c.fk_stu_id ='56'
but the output was
pk_route_id route_code route_name route_description
3 J1 IND IND-DPS
I am not sure where I am going wrong. Can you please enlighten me how to acieve this ?
If you want to get the not exists record then just filter out with IS NULL
SELECT a.*
FROM list_routes a
left outer join tbl_stu_route b on a.pk_route_id=b.fk_route_id
left outer join tbl_stu_class c on b.fk_stu_cls_id=c.pk_stu_cls_id
where b.fk_route_id is null;
Given the nature of your question, I would use not exists:
SELECT lr.*
FROM list_routes lr
WHERE NOT EXISTS (SELECT 1
FROM tbl_stu_route sr JOIN
tbl_stu_class c
ON sr.fk_stu_cls_id = c.pk_stu_cls_id
WHERE lr.pk_route_id = sr.fk_route_id AND
c.fk_stu_id = 56
);
Notes:
When using table aliases, use abbreviations for the table names. Abbreviations make the query much easier to follow than random letters.
Do not use single quotes for numeric constants. I assume that your ids are numbers, hence 56 rather than '56'.
I find that NOT EXISTS is a better fit to your problem description than LEFT JOIN, although both work.

How do I perform this join query?

I have a table leave_form which looks like:
type id reporting_id leave_bal from_date_id leave_from to_date_id leave_to number leave_for status applied_dates_id pendays
personal 99 6 10 1023 full day 1313 full day 10 personal yes 1026 null
I have separate table for dates, so that I can refer these dates into leave_form. My leave_date table looks like:
date_id(AI) dates(UK)
1025 2016-02-18
1301 2016-02-20
1218 2016-02-16
This date_id I have inserted into from_date_id, to_date_id, applied_dates_id columns in leave_form table i.e. all dates are inserted into leave_date table and from this table I am only referring the date_id into leave_form table.
There is also a table that keeps the emp_code and emp_name. My personal table is:
id(AI) emp_code(PK) emp_name
99 K0209 Nijo
When I am trying to fetch the date for from_date_id, to_date_id, applied_dates_id column from leave_form table I don't get any values.
My query for fetching the dates is:
select g.type, a.emp_code, h.rm_id, h.rm_name, g.leave_bal, i1.dates as from_date,
g.leave_from, i2.dates as to_date, g.leave_to, g.number, g.leave_for, g.status,
i3.dates as applied_date, g.pendays
from personal a
inner join leave_form g
on a.id = g.id
inner join inform_to_rm h
on h.reporting_id = g.reporting_id
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
where a.emp_code = 'K0209';
It shows me result like:
type, emp_code, rm_id, rm_name, leave_bal, from_date, leave_from, to_date, leave_to, number, leave_for, status, applied_date, pendays
i.e no data gets returned when I am executing this query.
I would agree with one of the comments to the question. I would recommend referencing the date directly in the leave_form table instead of a FK to a table with dates. But back to the question. You haven't described all of your tables completely, so it is possible that there are multiple problems that I can't see, however, there is definitely one problem.
Your query joins on
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
This is incorrect. leave_dates.dates is the actual DATE, while the columns that you are joining on (leave_form.from_date_id, leave_form.to_date_id, leave_form.applied_dates_id) are foreign key references.
For example, 1023 does not equal 2016-02-18 so you get no match. Replacing the above query-snippet with the following would correct this particular problem.
inner join leave_dates i1
on i1.date_id = g.from_date_id
inner join leave_dates i2
on i2.date_id = g.to_date_id
inner join leave_dates i3
on i3.date_id = g.applied_dates_id

LEFT JOIN with a condition

I need to join two tables. I only want to show matches where users.private = 0
feeds
id user_id
100 1
101 NULL
102 2
users
id private
1 1
2 0
I have seen many related questions, and the answer is always to move the WHERE condition into ON instead. But if i do this:
SELECT `feeds`.`id`, `feeds`.`user_id` AS `feed_user_id`, `users`.`id` AS `user_id`, `users`.`private`
FROM `feeds`
LEFT JOIN `users` ON `feeds`.`user_id` = `users`.`id`
AND `users`.`private` = 0
This returns
id feed_user_id user_id private
100 1 NULL NULL
101 NULL NULL NULL
102 2 2 0
What I WANT it to do is exclude the first row id 100. (So I guess that is not really a LEFT JOIN -> I want it to LEFT JOIN if the condition is met, otherwise exclude) How can I do this?
You need two separate condition clauses, one on the JOIN and one on the SELECT, like so:
SELECT `feeds`.`id`, `feeds`.`user_id` AS `feed_user_id`, `users`.`id` AS `user_id`, `users`.`private`
FROM `feeds`
LEFT JOIN `users` ON `feeds`.`user_id` = `users`.`id`
WHERE `users`.`private` = 0 OR `feeds`.`user_id' IS NULL
The problem with your current approach is that the join is treating private users as if they don't exist, rather than saying "this person exists and I should exclude this row from the results".

MySQL join & search

I have a problem with joining some tables, heres my structure:
tbl_imdb:
fldID fldTitle fldImdbID
1 Moviename 0000001
tbl_genres:
fldID fldGenre
1 Action
2 Drama
tbl_genres_rel:
fldID fldMovieID fldGenreID
1 1 1
2 1 2
What I’m trying to do is a query that will find all movies that is both an action movie and drama, is this possible to do without a subquery, if so, how?
What I'm trying right now is:
SELECT tbl_imdb.*
FROM tbl_imdb
LEFT JOIN tbl_imdb_genres_rel ON ( tbl_imdb.fldID = tbl_imdb_genres_rel.fldMovieID )
LEFT JOIN tbl_imdb_genres ON ( tbl_imdb_genres_rel.fldGenreID = tbl_imdb_genres.fldID )
WHERE tbl_imdb_genres.fldGenre = 'Drama'
AND tbl_imdb_genres.fldGenre = 'Action';
But this dosnt work, however it does work if I only keep one of the two WHERE's, but thats not what I want.
Two ways to do it:
1
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel rel_action
ON tbl_imdb.fldID = rel_action.fldMovieID
INNER JOIN tbl_genres genre_action
ON rel_action.fldGenreId = genre_action.fldID
AND 'Action' = genre_action.fldGenre
INNER JOIN tbl_genres_rel rel_drama
ON tbl_imdb.fldID = rel_drama.fldMovieID
INNER JOIN tbl_genres genre_drama
ON rel_drama.fldGenreId = genre_drama.fldID
AND 'Drama' = genre_drama.fldGenre
This method is on the same path as your original solution. 2 differences:
The join should be inner, not left because you're trying to get movies that certainly have the corresponding genre entry
Since you want to find 2 different generes, you'll have to do the join with tbl_genres_rel and tbl_genres twice, once for each particular genre you're interested in.
2
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel
ON tbl_imdb.fldID = tbl_genres_rel.fldMovieID
INNER JOIN tbl_genres
ON tbl_genres_rel.fldGenreId = tbl_genres.fldID
AND tbl_genres.fldGenre IN ('Action', 'Drama')
GROUP BY tbl_imdb.fldID
HAVING COUNT(*) = 2
Again, the basic join plan is the same. Difference here is that we join to the tbl_genres_rel and tbl_genres path just once. This on itself fetches all genres for one film, and then filters for the one's you're interested in. The ones that qualify will now have 2 rows for each distinct value of tbl_imdb.fldId. The GROUP BY aggregates on that, flattening that into one row. By asserting in the HAVING clause that we have exactly 2 rows, we ensure that we keep only those rows that have both the genres.
(Note that this assumes that there is a unique constraint on tbl_genres_rel over {fldMovieID, fldGenreID}. If such a constraint is not present, you should consider adding it.)
LEFT JOIN is not applicable in your case because records should exist on both tables. And you need to count the instances of the movie
SELECT *
FROM tbl_imdb a
INNER JOIN tbl_genres_rel b
on a.fldID = fldMovieID
INNER JOIN tbl_genres c
on c.fldGenreID = b.fldID
WHERE c.fldGenre IN ('Drama', 'Action')
GROUP BY a.Moviename
HAVING COUNT(*) > 1

How do I do 2 unique LEFT JOINs on the same table cell?

In mysql I'd like to do 2 unique LEFT JOINs on the same table cell.
I have two tables.
One table lists individual clients and has a clientNoteID and staffNoteID entry for each client. clientNoteID and staffNoteID are both integer references of a unique noteID for the note store in the notesTable.
clientsTable:
clientID | clientName | clientNoteID | staffNoteID
notesTable:
noteID | note
I'd like to be able to select out of the notesTable both the note referenced by the clientNoteID and the note referenced by the staffNoteID.
I don't see any way to alias a left join like:
SELECT FROM clientsTable clientsTable.clientID, clientsTable.clientName, clientsTable.clientNoteID, clientsTable.stylistNoteID
LEFT JOIN notes on clientTable.clientNotesID = notes.noteID
LEFT JOIN notes on clientTable.staffNoteID = notes.noteID as staffNote
(not that i think that really makes too much sense)
So, how could I query so that I can print out at the end:
clientName | clientNote | staffNote
When you join a table the alas must be immediately after the table name, not after the join condition. Try this instead:
SELECT clientsTable.clientName, n1.note AS clientNote, n2.note AS staffNote
FROM clientsTable
LEFT JOIN notes AS n1 ON clientTable.clientNotesID = n1.noteID
LEFT JOIN notes AS n2 ON clientTable.staffNoteID = n2.noteID
you need to alias the tables themselves
SELECT FROM clientsTable clientsTable.clientID, clientsTable.clientName, clientsTable.clientNoteID, clientsTable.stylistNoteID
LEFT JOIN notes a on clientTable.clientNotesID = a.noteID
LEFT JOIN notes b on clientTable.staffNoteID = b.noteID
SELECT CT.clientName, N1.note AS clientNote, N2.note AS staffNote
FROM clientsTable CT
LEFT JOIN notes N1 on CT.clientNotesID = N1.noteID
LEFT JOIN notes N2 on CT.staffNoteID = N2.noteID