LEFT JOIN to show NULL rows + WHERE - mysql

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

Related

Left Join to find difference between two tables with different id

I have two mysql tables: table_old and table_new, both with columns:
name,
surname,
birthdate
birthplace
plus others info columns. Both have an id column that doesn't match between tables. Some records are changed over time between tables, and now I need to have a third table with the records that are changed (added or deleted) between tables.
So I need to compare tables with name and surname and birthdate and birthplace.
I think that I have to use a Left Join, but I'm not sure abut syntax. Any help?
MySQL does not actually support a formal OUTER JOIN operation, but we can simulate one using a union of two joins. Use a full outer join to include records from both tables which do not match to anything in the other table:
SELECT t1.*, 'table one' AS missing
FROM table_old t1
LEFT JOIN table_new t2
ON t1.name = t2.name AND
t1.surname = t2.surname AND
t1.birthdate = t2.birthdate AND
t1.birthplace = t2.birthplace
WHERE t2.name IS NULL
UNION ALL
SELECT t2.*, 'table two'
FROM table_old t1
RIGHT JOIN table_new t2
ON t1.name = t2.name AND
t1.surname = t2.surname AND
t1.birthdate = t2.birthdate AND
t1.birthplace = t2.birthplace
WHERE t1.name IS NULL;
My criteria for claiming that a record is out of date is that any one of the fields does not match. That is, if three fields agree, but one does not, then I do not count it as a match.
Follow the link below for a running demo. You might be able to refine my answer and make it easier to group together pairs of records which are logically the same, but that would take more work.
Demo

Select from two tables in MySQL

I have two tables with users and I want to select the users from the first table which do not exist in the second. Can you help me?
When I use the code
Select t1.user_name From t1 Inner Join t2 On t1.user_name != t2.user_name;
I get all the users many times (actually as the number of the users - 1).
Use a LEFT JOIN instead like
Select t1.user_name From t1 left join t2
On t1.user_name = t2.user_name
where t2.user_name is null;
You can use EXISTS like this
SELECT t1.user_name FROM t1 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE t1.id = t2.id)
This example assumes that you have some sort of ID on the tables that represents the primary key and foreign key.
Not sure how are your tables designed, but having the same info (user_name) in more than one table is considered as duplication of data. To fix this, you should read about Database normalization

Join two tables on two columns, even if table 2 does not have row

What I am trying to do is join two tables, lets call them t1 and t2 on two columns. id and name, for this example. t1 will always have id and name, but t2 won't always have id and name. t1 has more columns like viewes, reports, and t2 has other columns that need to be joined. My question is, how can I show 0's for t2's columns if they don't exist?
I hav something similar to this, that joins tables only if both tables' rows have some value.
SELECT
date(t1.start_time) date,
t1.name,
t1.viewes,
t1.reports,
t2.col5,
t2.col6
from
table1 t1
left outer join table2 t2
on t2.name = t1.name and date(t2.start_time) = date(t1.start_time)
group by
1,2
order by
1 desc,
2 asc
;
I have lot's of experience with MySQL, but sometimes find that things need to be hacked to work correctly. What's your suggestion for this problem?

inner join for a query?

I want to do a sql query and have some problems:
I want to salect from table_1 the ID's Where parent_id is the value I have:
SELECT ID FROM table_1 WHERE parent_ID = 'x'
I want to use the ID'S I got in 1. and
SELECT FROM table_2 WHERE ID = 'The ID's from Query 1.
Simple and Execute
Select t1.`id` FROM table t1 INNER JOIN table t2 ON t1.`id`=t2.`id`
As Bainternet mentioned you can do this with a subquery
SELECT * FROM table_2 WHERE ID IN (SELECT ID FROM table_1 WHERE parent_ID = 'x')
Though your idea to use an inner join is also good (especially since MySQL can be slow when processing subqueries).
SELECT t2.* FROM table_2 as t2 INNER JOIN table_1 AS t1 ON t2.ID = t1.ID WHERE t1.parent_ID = 'x'
If that's not clear, try looking at the MySQL JOIN Syntax or the Subqueries, as Bainternet mentioned. If these examples and the MySQL docs aren't clear enough for you, consider posting more details on exactly what you're trying to do (e.g. include table structures in your question). Also while you might want this information for some WordPress related work you are doing, there's nothing in the question itself that actually ties it to WordPress. So if you have more questions that about MySQL queries in general, then you might want to consider posting them to the StackOverflow, tagged as mysql-query.

How to get data from 2 mysql tables

what is the syntax if I want to utilize 2 or more tables for my mysql query.
Example, I'm going to fetch the idnumber from the 1st table and the religion on the 2nd table. And the query will return the combined version of those 2 tables showing only the religion and idnumber.
The code might look something like this , but it doesn't work:
select t1.IDNO, t1.LNAME t2.RELIGION from t1, t2 where t2.IDNO='03A57'
The SQL query would be as follows:
SELECT a.idnumber, b.religion FROM table1 a, table2 b
You can add conditions from both tables as well by doing the following:
SELECT a.idnumber, b.religion FROM table1 a, table2 b WHERE b.religion = 'Christian'
More information can be found in this thread: http://www.astahost.com/info.php/mysql-multiple-tables_t12815.html
SELECT t1.IDNO, t1.LNAME FROM t1 LEFT JOIN t2.RELIGION ON ( t2.IDNO = t1.IDNO )
(more or less)
The Join is the command that will link the two.
http://dev.mysql.com/doc/refman/5.0/en/join.html
The code below would do a cross-join.
SELECT tb1.id, tb2.religion FROM tb1 JOIN tb2 ON (tb1.religion_id = tb2.religion_id) WHERE t2.IDNO='03A57'
Again... see http://dev.mysql.com/doc/refman/5.0/en/join.html...