MYSQL IN statement with column values from another table - mysql

I want to get specific column values from one table and use those values to get information from another table.
I'll use this example: both table1 and table2 contains rows with a "name" column.
I want to select the values of all the "name" columns from table1 meeting conditions and then select rows from table2 which contain any of the names selected from table1. The basic idea is below.
SELECT `name` FROM table1 WHERE...
SELECT `name` FROM table2 WHERE `name` IN(names from the above query)
Hopefully this is clear, thanks.

Just "inject" your first query in the second :
SELECT name FROM table2 WHERE name IN(SELECT name FROM table1 WHERE...)

Apart from the answer from KayKay you can also use EXISTS:
SELECT name FROM table2 t2
WHERE EXISTS( SELECT 1
FROM table t1
WHERE t2.name = t1.name)

Related

Update/Insert column values with another table's group by result in mysql

I have an empty table (t1) and I want to insert or update the t1.uid column from another table's (t2) GROUP BY uid values.
So far I have tried like this:
UPDATE table1 t1 JOIN
(SELECT uid FROM table2 GROUP BY uid) t2
SET t1.uid = t2.uid;
but it's not working for me.
N.B. I've got a massive data set for which group by (uid from table-t2) results giving me total 1114732 results which I have to insert/update in t1 table's uid column.
Please try this:
Insert into table1(uid)
select distinct uid from table2
If table1 is empty, then UPDATE is not the correct verb. Would this suit your needs?
INSERT into table1 SELECT distinct uid from table2;
INSERT ... SELECT docs

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

Compare data of two tables, store common data in a third, else in fourth table

I have four tables having same structure. Say they have a column name in them. I need to check if values in column name of table1 is present in column name of table2.
If present, I need to insert this name in column name of table3, else in column name of table4.
Assuming INSERT and Postgres ...
I would use a data-modifying CTE (Postgrs 9.1+) with one SELECT and two INSERT:
WITH cte AS (
SELECT t1.name, t2.name IS NULL AS t2_missing
FROM table1 t1
LEFT JOIN table2 USING (name)
)
, ins AS (
INSERT INTO table3 (name)
SELECT name FROM cte WHERE NOT t2_missing
)
INSERT INTO table4 (name)
SELECT name FROM cte WHERE t2_missing;
The same is not possible in MySQL (no CTEs, not to mention writable CTEs).

SELECT * FROM table1 LEFT JOIN table1.value AS table2

i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view

Mysql Load two table column value into Single

I have two table in database.
Table1 -> Name
Table2 -> Name
What will be query to get all the "Name" from Table1 and Table2 into single Column.
This query returns the value from the Name column from Table1 and the Name column from Table2, concatenated together into a single resultset.
SELECT t1.Name FROM Table1 t1
UNION ALL
SELECT t2.Name FROM Table2 t2
(This was my understanding of what you were looking for.)
If you want just a "distinct" list of Name values (exclude duplicate occurrences of the same value), then remove the ALL keyword.
If I correctly understood
http://dev.mysql.com/doc/refman/5.0/en/union.html
Select name from table1
union
Select name from table2
You can select data from two tables like this.
SELECT CONCAT(table1.name,table2.name) as Name FROM table1,table2;
and in case table1.name is A and table2.name is b you get
Name = AB
SELECT Name FROM Table1 NATURAL LEFT JOIN Table2 AS t2.
This will give you a list of only non-duplicate names from Table1 and Table2.