Mysql Load two table column value into Single - mysql

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.

Related

How do I include table names in a MySQL query?

This seems like it would be a simple setting, but I cannot find it. I do inner join queries on tables that have similar column names. It would be nice to include the table name in the query results, so the people receiving the data can differentiate more easily. For example:
Table1:
id
name
timestamp
Table2:
id
name
timestamp
table1_id
Table3:
id
name
timestamp
table2_id
Then I tie it all together with a query:
select * from table1
inner join table2 on table1.id=table2.table1_id
inner join on table2.id=table3.table2_id;
The results have similar column header names:
id name timestamp id name timestamp table1_id id name timestamp table2_id
It's hard to tell the data apart. Of course the example query is short and silly and pointless. If I do an actual query with all the data it get more complicated. Couldn't the column header name include the table name?
table1.id table1.name table1.timestamp table2.id table2.name table2.timestamp table2.table1_id table3.id table3.name table3.timestamp table3.table2_id
You have ambiguous column names in output: table1.id, table2.id
Adding alias for columns should solve this:
SELECT table1.id as t1_id, table2.id as t2_id
Instead of writing
select * from
you can write
select table1.id as table1_id,
and do the same for the other columns so that the results set would show you the names you give yourself for each column
You can use aliases to identify columns:
SELECT table1.id AS table1_id FROM ...
But you would have to do this for each field you want to select.
try this.hope it will help you.
SELECT table1.id as t1_id, table2.table1_id as t2_id
FROM tablename
inner join table2 on table1.id=table2.table1_id
inner join table3 on table2.id=table3.table2_id;

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

MYSQL count first table ids where value is found in second table

I have two tables,
table1 and table2 both tables has these columns
id, name, rel_id
now i would like to have a query to count ids of the table1 where name from table2 is equals to john and table1 rel_id equals to table2 rel_id.
so something like this (this is not correct that's why i need help to make it work).
Select count(ids) from table1
where table2.name="john"
and table1.rel_id=table2.rel_id
Well, one way is to use a join:
Select count(t1.id)
from table1 t1 join
table2 t2
on t1.rel_id = t2.rel_id
where t2.name = 'john';
Note that this uses table aliases to distinguish all the columns in each table. Because the tables have the same columns, you need to identify the table for each column. Also, the I changed the string constant to use single quotes rather than double quotes.
You need to look into joins so :
select count(ids)
from table1 join table2 on table1.rel_id=table2.rel_i
where table2.name="john"
A short intro from W3C schools: http://www.w3schools.com/sql/sql_join.asp
The full MySQL URL for more reference http://dev.mysql.com/doc/refman/5.0/en/join.html

MYSQL IN statement with column values from another table

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)

How can I get data from from two different tables in a single MySQL query?

I want to get data from two tables in my database. Here's what my tables might look like:
table 1 with fields id, author, profession, country
table 2 with fields id, quote, author, category
I want to select quote and author from table 2 and the corresponding profession from table 1, with the same author in both tables.
How do I construct a query that does this?
Supposing that your author column contains unique identifiers for authors, try:
SELECT t2.quote, t2.author, t1.profession
FROM table2 t2
LEFT JOIN table1 t1 ON t2.author = t1.author
select T2.quote, T2.author, T1.profession
from table1 T1, tabel2 T2
where T1.id = T2.id
SELECT table2.quote, table2.author, table1.profession FROM table2, table1 WHERE table2.author=table1.author
you can add LIMIT 1 at the end to have single result.