I am a bit stunned that I am no able to produce my results I want to get so I ask you experts for help!
I have three Tables showing only the important parts:
T1: (List of all names and schedules assigned)
Name | ScheduleName
T2: (all possible schedule names)
ScheduleName
T3: (List of all names)
Name
I try to find the ScheduleName per Person that was not assigned. In the end I would like to see:
Name1 | ScheduleName1
Name1 | ScheduleName2
Name1 | ScheduleName3
Name2 | ScheduleName2
What I tried:
SELECT
T2.ScheduleName
FROM
T2
WHERE
T2.ScheduleName NOT IN
(SELECT T1.ScheduleName FROM T1 WHERE T1.Name = "Name1")
This gives me the not assigned schedules for Person Name1. Works.
If I remove the WHERE statement within the parenthesis it returns no rows. (Because it matches it against the whole dataset)
Help very much appreciated
Thanks in advance
EDIT: Tried to work with a LEFT JOIN but it is not returning rows with "NULL" although it should
Here is one option, using a cross join:
SELECT
T3.Name,
T2.ScheduleName
FROM T2
CROSS JOIN T3
LEFT JOIN T1
ON T2.ScheduleName = T1.ScheduleName AND T3.Name = T1.Name
WHERE
T1.Name IS NULL;
The idea behind the cross join is that it generates all possible pairings of names and schedules. That is, it represents the entire set of all pairings. Then, we left join this to the T1 assignment table to find pairings which were never made.
We could also have achieved the same thing using EXISTS logic:
SELECT
T3.Name,
T2.ScheduleName
FROM T2
CROSS JOIN T3
WHERE NOT EXISTS (SELECT 1 FROM T1
WHERE T2.ScheduleName = T1.ScheduleName AND T3.Name = T1.Name);
Related
I have TWO tables:
t1 contain ID_car (unique), name
t2 contain ID_car(from t1), status (many status records on same ID_car)
And i need the following result:
All ID_car FROM t1 WITHOUT status = something
I already try it INNER, LEFT, RIGHT JOIN and didn't work.
How can i do that?
Many thanks for help!
More details:
t1
------------
ID_car name
------------------
1 Toyota
2 Honda
3 Mazda
4 Ford
t2
-----------------
ID_car status
1 ok
1 not_ok
2 ok
4 not_ok
ID_car 3 din not have any records in t2 but i want to display result
And i need the following result (all car from t1 without car status not_ok):
the expected result
-----------------
ID_car status
2 ok
3
Update 2
Finally solved! Thanks for help!
That's works for me:
SELECT * FROM t1
WHERE t1.ID_auto NOT IN
(SELECT DISTINCT t1.ID_auto FROM t1, t2 WHERE t1.ID_auto = t2.ID_auto AND t2.category='not_ok')
-- updated per comment below. This update will return all records from T2 (even if not existing in T1) but only if T2.Status != something
-- this should do what you want. It will give you all the records in T1, and any data in T2 (but not required to be in that table) where your T1.status is not something
SELECT *
FROM t1
LEFT JOIN t2 ON T1.ID = T2.ID
-- did the logic on the JOIN here instead of in where clause, because doing in where clause would force records to appear in t2 table (basically converting it to an inner join) doing it in the join itself does not cause this to happen
AND T2.Status != 'something'
SELECT ID_car
FROM t1
LEFT JOIN t2
ON t1.ID_car=t2.ID_car
WHERE t2.status=something
SELECT ID_car
FROM t1
LEFT JOIN
t2 on t1.ID_car=t2.ID_car
WHERE NOT t2.status='something'
Created from top of my head, hopefully it works!
Maybe if you could post the queries you wrote, maybe they could show us why the joins don't work because it should.
I do not know if I understand correctly but try and let me know:
SELECT DISTINCT t1.ID_car FROM t2 INNER JOIN t1 ON t2.ID_car = t1.ID_car WHERE t2.status != 'something'
I have two tables, one which contains unique names (bill_datatypes), and another one which expresses that these names are connected to some subcategories (one name can be connected to more categories as well).
The first table (relevant part):
The second table (relevant part):
I would like to select all the names which are not connected to a specific subcategory yet. This means that the names might be mentioned in the connecting table but connected to a different subcategory.
I have solved the problem with the following subquery:
SELECT T1.name
FROM bill_datatype T1
WHERE NOT EXISTS (
SELECT T2.bill_datatype_id
FROM obligatory_field T2
WHERE T2.bill_datatype_id = T1.id AND T2.bill_sub_category_id = 1
)
This solution seems to work well, but this way I can not have any data from the second table, which might be needed in the future.
Do you have any suggestions on how to solve the problem with a LEFT JOIN? (The WHERE clause on the subcategory made it too challenging for me.)
Thank you for your help also in advance!
From how I understand your question, I think this query will achieve what you want:
SELECT T1.name
FROM bill_datatype T1
LEFT JOIN obligatory_field T2
ON T2.bill_datatype_id = T1.id AND T2.bill_sub_category_id = 1
WHERE T2.bill_datatype_id IS NULL
The WHERE condition on the JOIN'ed table will give you only the names from T1 which have no bill_sub_category_id or their bill_sub_category_id is not 1.
SQLFiddle
SELECT distinct T1.name
FROM
bill_datatype T1
right join
(
SELECT T2.bill_datatype_id
FROM obligatory_field T2
WHERE T2.bill_sub_category_id = 1
)
as T3
on T1.id != T3.bill_datatype_id
http://sqlfiddle.com/#!9/d4f616/18
If I have two tables that I'm joining and I write the most simple query possible like this:
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
There are a few records who have multiple rows per ID because they have multiple employers, so t1 looks like this:
ID Name Employer
12345 Jerry Comedy Cellar
12345 Jerry NBC
12348 Elaine Pendant Publishing
12346 George Real Estate
12346 George Yankees
12346 George NBC
12347 Kramer Kramerica Industries
t2 is linked with the similar IDs but with some activities that I'd like to see -- hence the SELECT * above. Though I don't want multiple rows to return if the Employer column is "NBC" -- but everything else is good.
The only other thing that matters here is that t2 is smaller than t1, because t1 is everybody and t2 are only from people who did particular activities -- so some of the matches won't return anything from t2, but I would still like them to be returned, hence the LEFT JOIN.
If I write the query like this:
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
WHERE Employer <> "NBC"
Then it removes Jerry and George completely -- when really all I want is for the NBC row to not be returned, but to return any other rows that are associated with them.
How can I write the query while joining t1 with t2 to return each row except for the NBC ones? The ideal output would be all of the rows from t1 regardless if they match up with all of t2 except removing all of the rows with "NBC" as the employer in the return file. Basically the ideal here is to return the JOINs where they fit, but regardless remove the entire row for anybody with "NBC" as employer without removing their other rows.
The more I write about it, it seems like I should potentially just run a query prior to my JOIN to delete all the rows in t1 who have "NBC" as their employer and then run the normal query.
Basic subset filtering
You can filter either of the two merged (joined) subsets by extending the ON clause.
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer != 'NBC'
If you get null values now, and you don't want them, you'd add:
WHERE t2.Employer IS NOT NULL
extended logic:
SELECT *
FROM t1
LEFT JOIN t2
ON (t1.ID = t2.ID AND t2.Employer != 'NBC')
OR (t2.ID = t2.ID AND t2.Employer IS NULL)
Using UNION
Basically, JOIN is for horizontal linking and UNION does vertical linking of datasets.
It merges to resultsets: the first without NBC, and the second (which is basically an OUTER JOIN), adds everyone in t1 which is not part of t2.
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer != 'NBC'
UNION
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer IS NULL
String manipulation in the resultset
If you just want to remove NBC as a string, here is a workaround:
SELECT
t1.*,
IF (t2.Employer = 'NBC', NULL, t2.Employer) AS Employer
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
This basically replaces "NBC" by NULL
I have been looking and trying to join two tables to get one result but with no luck. (yes a newbie)
Here is the scenario:
Table T1
ID | t1_name | **t1_value**
Table T2
ID | t2_something | **t2_name** | t2_more | t2_etc
What I am trying to do is a SELECT query to get a result to ECHO the t1_value.
This is from t2_name equaling the same as t1_name then return the t1_value to ECHO.
These are based on each indivdual "t2 ID" (100 + ids) having its own result.
Im sure this is an easy solution, and all the reading and research I have done on joins i just cant seem to get my head around it. Maybe over reading....
If you can offer a solution/help, (with how it works so I can learn for next time)
That would be appreciated.
JOIN the two tables with the condition t1.t2_name = t2.t2_name, then in the SELECT clause select whatever you want to select.
Something like:
SELECT
t2.ID,
t1.t1_value,
...
FROM t1
INNER JOIN t2 ON t1.t1_name = t2.t2_name;
You might also need to have a look at the different types of JOIN, see this for more information:
A Visual Explanation of SQL Joins.
Try this query
select t1.t1_value
from t1
inner join t2 on t1.t1_name=t2_name
For more study you can refer INNER JOIN
And Different type of JOIN
A VISUAL REPRESENTATION OF JOIN
Select t1_value from T1, T2 where T1.t1_name = T2.t2_name
SELECT t1_name.t1, t2_name.t2 FROM t1, t2 WHERE t1.t1_name = t2.t2_name
That should work as expected, just to clarify you can also use INNER JOIN syntax as referenced here : http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php
You can achieve by just joining two tables t1 and t2.
SELECT
t1.Name,
t1.value
FROM t1,t2
WHERE t1.t1_name = t2.t2_name
I have 2 rows of data in a mysql database. Both rows are identical with the exception of one column. What I am looking for is a query that will produce a single row with both distinct values. Can someone please help me out?
An example of the data is:
1 Administrator Test 2009-08-17 03:57:35
1 Miller Test 2009-08-17 03:57:35
What I need is this:
1 Administrator Miller Test 2009-08-17 03:57:35
DISTINCT won't give it to me. I need the additional column added or appended to the result set.
This is the code I am trying to use.
SELECT
t2.msg_id,
t3.lname AS sender,
t4.lname AS recipient
FROM
mail_message AS t1
Inner Join mailbox AS t2 ON t1.msg_seq = t2.msg_seq
Inner Join employee AS t3 ON t3.employee_id = t2.employee_id
INNER JOIN employee AS t4 ON t3.employee_id = t4.employee_id
Sure. You can JOIN the table onto a copy of itself, with a technique something like this:
SELECT t1.*, t2.name
FROM the_table AS t1
INNER JOIN the_table AS t2 ON (t1.id = t2.id AND t1.test = t2.test AND t1.date_column = t2.date_column AND t1.name < t2.name)
Here we arbitrarily name one copy of the table t1 and the other t2 and join rows that are identical except for the name.
Note that I do not simply do a != test on the name, since that would result in two matching rows: one where Administrator is in t1 and the other where Administrator is in t2. To distinguish one identical table from the other, I assert that t1 will always be the one with a "smaller" (i.e., earlier alphabetically) name.
If there can be three or more identical rows, this will still work, but will return several matches. For example, if A, B, and C are all names in otherwise identical rows, you'd get:
A B
B C
A C