Reverse effect of Join in mysql [duplicate] - mysql

This question already has answers here:
MYSQL Left Join how do I select NULL values?
(4 answers)
Closed 4 years ago.
I'm developing a system and creating invoices. I want the record where no invoice has been created.
I'm trying to write a MySQL query, where I need the records whch can not be joined with another table. In other words, the records that do not have a linked record in the other table.
I tried the following query
SELECT exports.id as e_id,export_invoices.id as i_id
FROM exports
LEFT JOIN export_invoices ON export_invoices.export_id = exports.id
and got this result:
Which gives all value and also the record of which invoice is not created with NULL value (I want to have that [e_id->2 from result]). I just want to extract that null value record's master id.

Simply add the where condition in your query -
SELECT exports.id as e_id,export_invoices.id as i_id
FROM exports LEFT JOIN export_invoices on export_invoices.export_id = exports.id
WHERE export_invoices.id IS NULL;

Related

Mysql condition based table join [duplicate]

This question already has answers here:
Filter Table Before Applying Left Join
(4 answers)
Closed 2 years ago.
I want to join two tables based on the below scenarios:
Need to display all the requirements table values
Join the second table(prod_spec) based on the requirement_id and inquiry_id.
Table 1(requirements):
Table 2(prod_spec):
Using below query I'm getting the result below:
SELECT requirements.id,requirements.requirement, prod_spec.evidence,prod_spec.status,prod_spec.no_specify,prod_spec.inquiry_id FROM requirements LEFT JOIN product_spec ON prod_spec.requirement_id=requirement.id
The problem is not getting any result when I put the where condition eg: where inquiry_id='67' to the sql query. Please let me know how to display the rows based on inquiry_id.
FYI, requirement_id will only there once in prod_spec table based on inquiry_id.
The problem is not getting any result when I put the where condition eg: where inquiry_id = '67' to the sql query
The condition on the LEFT JOINed table needs to go to the ON clause of the JOIN:
SELECT r.id,r.requirement, ps.evidence,ps.status, ps.no_specify, ps.inquiry_id
FROM requirements r
LEFT JOIN prod_spec
ON ps.requirement_id = requirement.id
AND ps.inquiry_id = 67 --> here
Rationale: if you put that condition in the WHERE clause, then it becomes mandatory; as a consequence, rows from requirements for which there is no match in prod_spec are evicted from the resultset.
Side notes:
table aliases make the query easier to write and read
it seems like inquiry_id is a number, so it should be compared as such; don't put single quotes around literal 67

MySQL - How to left join distinct records based on fields efficiently (no n+1)? [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 3 years ago.
We have two customer related tables below and a query with red rows for the undesired results. We are trying to get the results for each customer in customer_data to include their respective latest phone_number (from the customer_phones table based on theupdated field) that is default and not deleted (shown in query below). The customer_data.customer_id is the same as customer_phones.user_id and where we do the left join on.
1) DB table name/data
customer_data
customer_phones
2) Query
SELECT cd.id, cd.customer_id, cd.customer_name, cp.phone_number, cp.updated
FROM customer_data cd
LEFT JOIN customer_phones cp ON cp.user_id=cd.customer_id
AND cp.is_default_phone='1' AND cp.deleted IS NULL
ORDER BY cd.id
3) Results (with undesired rows in red)
The red rows are undesired since each customer should only have one phone number that is both default and updated latest (in case they have more than one default number the one with the latest updated would be the correct one to pull into the results).
We expect these table to have a lot of fields in a few months and want to make sure we don't have a solution with an n+1 issue if using a subquery of some sort. So what would be the most efficient way to query the tables to get the desired results above but without the red undesired rows?
You have to set one more condition in the ON clause to get the latest updated row only:
SELECT cd.id, cd.customer_id, cd.customer_name, cp.phone_number, cp.updated
FROM customer_data cd
LEFT JOIN customer_phones cp
ON cp.user_id=cd.customer_id AND cp.is_default_phone='1' AND cp.deleted IS NULL AND
cp.updated = (
select max(updated) from customer_phones
where user_id=cd.customer_id AND is_default_phone='1' AND deleted IS NULL
)
ORDER BY cd.id

Can't update existing records MySQL [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 4 years ago.
I've created a new column (third one) and all records are null in that column. The table has 100 rows. When I try INSERT INTO, records are added but only from 101 row and previous ones are still null. That's why I decided to use UPDATE, but I have the next error message: You can't specify target table 'actors' for update in FROM clause
Here is my query:
UPDATE
actors
SET
starred_count = (
SELECT COUNT(actors.Actor_id)
FROM starred
INNER JOIN actors ON actors.Actor_id = starred.Actor_id
GROUP BY starred.Actor_id
ORDER BY starred.Actor_id
)
How could I do this properly?
Give a try to next query, since on MySQL updating using a query is not valid.
UPDATE
actors AS a
INNER JOIN
(SELECT Actor_id, COUNT(*) AS starred_count
FROM starred
GROUP BY Actor_id) AS s ON s.Actor_id = a.Actor_id
SET
a.starred_count = s.starred_count

MySql update with join from another table [duplicate]

This question already has answers here:
String concatenation in MySQL
(5 answers)
Closed 6 years ago.
I added a column to my WorkSheetTransaction table and want to populate it with a name built from the Department table. Both tables are already populated with the join field DepartmentId.
The following query runs ok but no rows are updated. Why not?
update WorkSheetTransactions
inner join Departments on WorkSheetTransactions.DepartmentId = Departments.DepartmentId
set WorkSheetTransactions.DepartmentName = (Departments.GL_Account + '-' + Departments.DepartmentName)
I've tried many variations but I just can't see where I've gone wrong. BTW, the join field is an integer in both tables and all the other 2 fields are var_chars.
In mysql, you should use concat to concatenate strings:
UPDATE WorkSheetTransactions
INNER JOIN Departments
ON WorkSheetTransactions.DepartmentId = Departments.DepartmentId
SET WorkSheetTransactions.DepartmentName = concat(Departments.GL_Account, '-', Departments.DepartmentName)

How to get records that exist in one MySql table and not another [duplicate]

This question already has answers here:
Mysql: Select rows from a table that are not in another
(10 answers)
Closed 9 years ago.
I need to write a query that returns the records that exist in one MySql table and do not exist in the other table. In this example, I want all of the wafers that exist in the wafer_log table that do not exist in the bt_log table.
Here is the current query I'm using:
SELECT wafer_log.wafer, bt_log.id AS blid, wafer_log.id AS wlid
FROM bt_log RIGHT OUTER JOIN wafer_log
ON bt_log.waferid = wafer_log.wafer
WHERE wafer_log.wafer IS NOT NULL AND bt_log.id IS NULL;
My thought here was to get the wafer name from the table I care about as well as the ids for both of the tables and do an outer join on the wafer name. From there, I wanted to see all of the results where wafer name was not null in wafer_log table and id in the bt_log is null.
I don't feel like the results look right though.
Any help would be appreciated.
You don't need a join to do this, assuming 'id' is your primary key you can use a subquery:
SELECT wafer_log.wafer, wafer_log.id AS wlid
FROM wafer_log
WHERE wafer_log.id NOT IN (SELECT id FROM bt_log);