Verifying existence of data accross tables - mysql

Lets say i have a database with the following two tables:
Table_A Table_B
id val_A
val ...
...
Now i need to do the following SELECT on Table A:
SELECT id, val, ..., isInB FROM Table_A WHERE ....
Where ... is any other fields from Table_A that i need and isInB will be either 1 or 0 depending on wheter or not Table_A.val exists in Table_B.val_A
Can this be done?

You just have to LEFT JOIN both tables:
SELECT ta.id,
ta.val,
CASE WHEN tb.val_A IS NULL THEN 0 ELSE 1 END AS isInB
FROM table_A ta
LEFT JOIN table_B tb ON ta.val = tb.val_A
See this fiddle.

Try this
SELECT Table_A.id, Table_A.val, IF(table_B.val_A IS NULL, 0, 1) as isInB
FROM
Table_A
LEFT OUTER JOIN Table_B on Table_A.val = Table_B.val_A

Related

Replace several entries

I have two similar tables with the same columns and same type of data. The goal is to get the data of table_a but replace all entries with table_b values if there are value with the same id.
So I have the following two selects:
SELECT table_a.id, table_a.text
and
SELECT table_b.id, table_b.text
In pseudo code:
iterate table_a data
if table_a.id == table_b.id then
table_out.id = table_b.id
table_out.text = table_b.text
else
table_out.id = table_a.id
table_out.text = table_a.text
end if
end iterate
The tables table_a and table_b data shall not be touched. Just the output of the select shall contain the data.
I was thinking first of joining the two tables with a left join. Then I would need a kind of if to switch columns for the select.
Thank you.
I would recommend coalesce() for this purpose:
select a.id,
coalesce(b.text, a.text) as text
from table_a a left join
table_b b
on a.id = b.id
order by a.id;
You can use LEFT JOIN it will give you all table_a record and common record between two tables. then use case logic to get require output
SELECT
t1.id,
(CASE WHEN t1.id=t2.id THEN t2.text ELSE t1.text END) AS text
FROM table_a t1
LEFT JOIN table_b t2
ON t1.id=t2.id
ORDER BY t1.id
DEMO

How do I do a Join

How do I make a join of the id_curso of these two tables:
In this table id_curso is a FOREIGN KEY from the second table
;
Second Table has id_curso has is Primary_key
For join two table with foreign key related in sample you provided .. inner join
return the subset for row with value common in each of the two table .. in this case with the same id_curso and this value must be present in each table .
If you need a simple inner join
select a.*, b.*
from table1 as a
inner join table2 as b on a. id_curso = b.id_curso ;
You can see a visual rapresenation an this link provided by #MichaelZ
http://imgur.com/hhRDO4d
I hope I didn't make any mistakes in here (using left join):
SELECT * FROM `first_table` LEFT JOIN `second_table` ON (first_table.id_Curso = second_table.id_Curso)
Here is a join statements it depends upon your scenerio
SELECT a.*, b.*
FROM TABLE1 as a
JOIN TABLE2 as b on a.id_curso = b.id_curso ;
SELECT a.*, b.*
FROM TABLE1 as a
LEFT OUTER JOIN TABLE2 as b on a.id_curso = b.id_curso ;
SELECT a.*, b.*
FROM TABLE1 as a
RIGHT OUTER JOIN TABLE2 as b on a.id_curso = b.id_curso ;

Select join statement is not working

I'm trying to make an output array from next statement:
I have 2 tables. In each table there is "material_code" which plays the main role. I want to select material_code from table1 where it'is equal to material_code from table2 and join them where status (from table2) is equal to 0.
This is what I've got so far.
SELECT material_code FROM table1
LEFT JOIN table2 ON material_code WHERE status IS 0
Solution:
SELECT material_code FROM table1
LEFT JOIN table2 ON table1.material_code = table2.material_code WHERE status = 0
Thank everyone.
You should try this one:
SELECT
material_code
FROM table1
LEFT JOIN table2
ON table1.material_code = table2.material_code
WHERE status = 0
Sounds like you want an INNER JOIN
SELECT material_code FROM table1
INNER JOIN table2 USING (material_code) WHERE status = 0
If you use LEFT JOIN, you will get records that only exist in table1 and not in table2. You also want to use USING not ON if the column name is the same in both tables (at least in MySQL).
WHERE status IS 0
should be
WHERE status = 0
Only when for null it have to be IS NULL.
And you also missed the join condition (ON table1.material_code = table2.material_code)
If you have a field 'status' in bot table, you can use an explicit table name before the field name to add a condition only on 'status' of table1 like :
WHERE table2.status = 0
Try this SQL code. INNER JOIN is better in your case.
SELECT tb1.material_code
FROM table1 as tb1
INNER JOIN table2 as tb2 ON tb1.material_code=tb2.material_code
WHERE tb2.status = 0

how to get results based on column's result type from 2 other tables

i have a table called users contained
(Table A)
Users
user_id
username
user_type[1,2]
(Table B) if user_type=1
user_id
full_name
(Table C) if user_type=2
user_id
full_name
i want to get single record set by executing single query, is that possible in PHP mysql.
Try this:
SELECT table_a.*, COALESCE(table_b.full_name,table_c.full_name) AS full_name
FROM table_a
LEFT OUTER JOIN table_b ON table_b.user_id = table_a.user_id
LEFT OUTER JOIN table_c ON table_c.user_id = table_a.user_id WHERE 1;
It uses the LEFT OUTER JOIN, which means that it joins it to table_b on the given condition. However, for each row in table_a, whether it finds a matching row in table_b or not, it will return the table_a row. If it does not find a matching row, the table_b columns are just NULL. Same thing with table_c.
Then, we just select all the table_a columns. However, we now have two full_name columns, one from table_b and one from table_c. We use COALESCE to merge them.
COALESCE returns the first non-NULL value.
Since we know that there is either a matching row in table_b or a matching row in table_c, it is not a problem. However, it would be a problem if somehow you allows a matching row to be found in both table_b and table_c.
The risk can be mitigated by adding additional ON clause conditions to get:
SELECT table_a.*, COALESCE(table_b.full_name,table_c.full_name) AS full_name
FROM table_a
LEFT OUTER JOIN table_b
ON table_b.user_id = table_a.user_id AND table_a.user_type = 1
LEFT OUTER JOIN table_c
ON table_c.user_id = table_a.user_id AND table_a.user_type = 2
WHERE 1;
Still, you will need to make sure only 1 row is present for each user in table_b and table_c.
Instead of COALESCE you can optionally use CASE like:
SELECT table_a.*, CASE user_type WHEN 1
THEN table_b.full_name
ELSE table_c.full_name END AS full_name
...
or use an IF function like:
SELECT table_a.*, IF(user_type=1,table_b.full_name,table_c.full_name) AS full_name
...
You can UNION both tables and later on JOIN it with tableA
SELECT a.User_ID,
a.`username`,
b.full_name,
a.user_type
FROM tableA a
(
SELECT user_ID, full_name
FROM tableB
UNION
SELECT user_ID, full_name
FROM tableC
) b ON a.User_ID = b.User_ID
-- WHERE a.user_type = 1 -- add extra condition here

Compare two tables MySQL

I have two similar tables table_a and table_b
table_a is my current data and I need to update this info using table_b which is a temporary table. The only difference between the two is table_a has a password field that table_b will not have.
I am trying to do a couple of things.
1.Compare the data based on the "user_id" field.
2. If there is a user_id not found in table_b but there is one in table_a remove that row in table_a
3. If there is a user_id in table_b that is not found in table_a add that row of data to table_a
4. If the user_id is found in both then check two of the fields "email" and "job_code" and make sure they are both whatever table_b says.
Should I do this in separate MySQL statements in the order I have numbered above? below is my try at the above statements. Any help or troubleshoot is greatly appreciated. Thanks!
Statement 1:
SELECT * FROM table_a
WHERE table_a.user_id
NOT IN (
SELECT table_b.user_id
FROM table_b
WHERE table_a.user_id=table_b.user_id
) // But how do I delete those records that are selected?
Statement 2:
SELECT * FROM table_b
WHERE table_b.user_id
NOT IN (
SELECT table_a.user_id
FROM table_a
WHERE table_a.user_id=table_b.user_id
) //How do I Insert these records that I have in table_b but not in table_a
Statement 3:
SELECT email FROM table_b
WHERE table_b.user_id = table_a.user_id,
AND table_b.email != table_a.email //now I need to update table_a with the emails from table_b
Statement #1
DELETE A.*
FROM table_a A
LEFT JOIN table_b B
USING (user_id)
WHERE B.user_id IS NULL;
Statement #2
INSERT INTO table_b (column1,column2,...,columnN)
SELECT A.column1,A.column2,...,A.columnN
FROM table_b B LEFT JOIN table_a A
USING (user_id)
WHERE A.user_id IS NULL;
Statement #3
UPDATE table_a A INNER JOIN table_b B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
UPDATE 2012-06-19 11:54 EDT
A and B are just aliases for the table.
For example, the query of Statement #3 could have been written like the following:
UPDATE table_a as A INNER JOIN table_b as B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
or with no aliases whatsoever, like this:
UPDATE table_a INNER JOIN table_b USING (user_id)
SET table_a.email = table_b.email,table_a.job_code = table_b.job_code;
The solution of the first problem ::
1)
delete FROM table_a
left join table_b
on
table_a.user_id=table_b.user_id
and table_a.user_id is null