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
Related
I have 3 tables of data I'm joining together. I'm using an ID column (day) in each table to join the data together. The following SQL query works, and provides the following column set:
SELECT *
FROM records
LEFT JOIN macro_nutrients ON macro_nutrients.day = records.day
LEFT JOIN micro_nutrients ON micro_nutrients.day = records.day
day|date|calories_in|calories_out|day|fat|sugar|protein|day|iron|cholesterol|potassium|vitamin_a|vitamin_c|
As I join tables, the "day" field (which works as my primary key for joining tables) gets outputted each time (3 tables would have three day fields listed). I would like to exclude the day fields from my column set, as they aren't needed.
Would this only be achievable by explicitly mentioning the fields I want in my select statement (eg. macro_nutrients.fat)? this would unfortunately get quite verbose if so. Curious if there a more compact way.
You can use USING:
SELECT *
FROM records r LEFT JOIN
macro_nutrients man
USING (day) LEFT JOIN
micro_nutrients mn
USING (day);
USING is SQL standard syntax that both SQLite and MySQL support. When * is used with USING, the columns used as JOIN keys are not duplicated in the result set. In an outer join, the value is the actual value in the data -- rather than the unmatched NULL value.
I have two tables and I want to fetch select columns from the tables.
table 1 is sfpinventoryinfo and table 2 is opticalportinfo.
Both have NEID as common.
SELECT
sfpinventoryinfo.NEID,
sfpinventoryinfo.SlotNumber,
sfpinventoryinfo.PortNo,
sfpinventoryinfo.PortType,
sfpinventoryinfo.`Type`,
sfpinventoryinfo.SN,
sfpinventoryinfo.GenDes,
sfpinventoryinfo.ApplicationCode,
opticalportinfo.ChannelFrequency
FROM
sfpinventoryinfo
JOIN
opticalportinfo ON sfpinventoryinfo.NEID = opticalportinfo.NEID;
But I am getting weird results:
As shows above result, Slot no 4 should have only 1 entry for port instead of 5
It's likely your opticalportinfo has six rows with the value 13 in NEID. So, your join produces all six rows in your result set.
It's hard to guess the "right" way to choose which of those six rows to use without knowing more about your application. You can hack around the problem with SELECT DISTINCT if you must. But it's a hack.
You clearly have duplicates in one or both tables. In your example data, the entire row looks duplicated, so you could use select distinct so entire rows are not repeated:
SELECT DISTINCT i.NEID, i.SlotNumber, i.PortNo, i.PortType, i.`Type`, i.SN,
i.GenDes, i.ApplicationCode, oi.ChannelFrequency
FROM sfpinventoryinfo i JOIN
opticalportinfo op
ON i.NEID = oi.NEID;
Or perhaps GROUP BY:
SELECT i.NEID, i.SlotNumber, i.PortNo, i.PortType, i.`Type`, i.SN,
i.GenDes, i.ApplicationCode, MAX(oi.ChannelFrequency)
FROM sfpinventoryinfo i JOIN
opticalportinfo op
ON i.NEID = oi.NEID
GROUP BY i.NEID, i.SlotNumber, i.PortNo, i.PortType, i.`Type`, i.SN,
i.GenDes, i.ApplicationCode;
That said, you really need to understand why there are duplicates and adjust your query or fix your data.
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;
This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 5 years ago.
Order Table
Product Table
I need to select all from these two tables. Is it possible?
With the following query you can select all data from one table:
SELECT * FROM Order;
If you want to select all data from two tables you can use a join in the select query to connect the data of the two tables:
SELECT * FROM Order
JOIN Product ON Order.p_id = Product.p_id;
There are different kinds of joins available. Based on the one you use can change the amount of data you will receive. The different kinds of joins are:
(Inner) join: Returns records that have matching values in both tables
Left (Outer) join: Return all records from the left table, and the matched records from the right table
Right (outer) join: Return all records from the right table, and the matched records from the left table
Full (outer) join: Return all records when there is a match in either left or right table
The information about the different kinds of joins comes from the following website:https://www.w3schools.com/sql/sql_join.asp
This question already has answers here:
Explicit vs implicit SQL joins
(12 answers)
Closed 8 years ago.
SELECT
dim_date.date, dim_locations.city, fact_numbers.metric
FROM
dim_date, fact_numbers
WHERE
dim_date.id = fact_numbers.fk_dim_date_id
AND
dim_locations.city = "Toronto"
AND
dim_date.date = 2010-04-13;
Since I'm not using the JOIN command, I'm wondering if this is indeed a JOIN (and if not, what to call it)?
This is for a dimensional model by the way which is using surrogate and primary keys to match up details
Yes, it is joining the tables together so it will provide related information from all of the tables.
The one major downfall of linking tables via WHERE instead of JOIN is not being able to use LEFT, RIGHT, and Full to show records from one table even if missing in the other.
However, your SQL statement is invalid. You are not linking dim_locations to either of the other tables and it is missing in the FROM clause.
Your query using an INNER JOIN which is comparable to your WHERE clause may look something like the following:
SELECT DD.date, DL.city, FN.metric
FROM dim_date AS DD
JOIN fact_numbers AS FN ON DD.id = FN.fk_dim_date_id
JOIN dim_locations AS DL ON DL.id = FN.fk_dim_locations_id
WHERE DL.city = 'Toronto'
AND DD.date = 2010-04-13
yes, it is joining tables. It is called non-ANSI JOIN syntax when join clause is not used explicitly. And when join clause is used, it is ANSI JOIN
If you reference two different tables in a where clause and compare their referential IDs, then yes, it acts the same as a JOIN.
Note however that this can be very inefficient if the optimizer doesn't optimize it properly see: Is there something wrong with joins that don't use the JOIN keyword in SQL or MySQL? and INNER JOIN keywords | with and without using them