I want to get all data from two table on different Databases.I've tried this and only get data from one table.
Select b1.branch.*, db2.branch.* from db1.branch,db2.branch
Is there any way to get that?
Assuming that your branch table have the same struct in both database then you could use an union eg:
Select db1.branch.*
from db1.branch
union all
select db2.branch.*
from db2.branch
for obtain both result sets
Related
I'm using MySQL and I selected three tables in one statement like that:
SELECT * FROM tb_i, tb_s, tb_t
But I need case control by their table names. If a row from tb_s it returns tb_s on additional column table_name.
How can I get table name from this statement?
The way you add up your tables now is a JOIN - meaning that every row is a Cartesian product of three of them - so each row is returned from all the three tables.
If you would like to concatenate the data from the three tables you should use UNION ALL which assumes that the columns are of the same structure. Then you would able to mark the origin table, with an addition constant field.
For instance in your case:
SELECT tb_i.*,'tb_i' as source
FROM tb_i
UNION ALL
SELECT tb_s.*,'tb_s'
FROM tb_s
UNION ALL
SELECT tb_t.*,'tb_t'
FROM tb_t
where the source column is a constant string per each table.
I have two tables, that have been structured identically but one is a log of older data and one tracks newer data entries. They are the same structure but the data entries are different (one is data before date x, one is data after date x).
My question is, is there a select statement (using MySQL) that can select all the data from both tables and list it as though it is one table? Essentially just listing the contents from table A, then listing the contents from table B in the same columns as though they are one table.
I could create another table that does this, but that would involve doubling the data size which isn't a scalable solution.
Thanks for any time you take on this!
Yes.
Union
Ex: SELECT * FROM LOG UNION SELECT * FROM LOG_OLD;
I have two tables with exactly the same schema.
I would like to have a 3rd table, containing all the data of these two tables combined.
How can I do this with an INSERT INTO query (single query doing this)?
I know I can do this with: INSERT INTOname_of_new_tableSELECT DISTINCT * FROMname_of_old_table but then I would need to do it twice. I am seeking to doing this with a SINGLE query.
Thank you.
INSERT INTO new_table
SELECT * FROM old_table_1
UNION
SELECT * FROM old_table_2
Removed your DISTINCT, as UNION has an implied DISTINCT (UNION ALL does not have this). This will only work if the tables are identical in column count and have similar column type, however. Then again, it sounds like that's your situation.
1000 Apologies if I've repeated a question, couldn't find an answer here to my question.
I'm try to retrieve the data from 2 separate columns from 2 unrelated tables in the same query.
I've tried using a UNION statement, but the problem is that I need to be able to separate the results into 'venues' and 'programmes' - here was what I did:
SELECT venue_name
FROM my_venues
UNION
SELECT programme_title
FROM my_programmes;
Maybe it's not necessary to combine the query and I can just do 2 separate queries? The database won't be especially large, but it seems unnecessary...
Help and thanks!
Just add a constant column in both selects, with the same name, but different values:
SELECT "venues" as source, venue_name as thing_name
FROM my_venues
UNION ALL
SELECT "programmes" as source, programme_title as thing_name
FROM my_programmes;
Now:
Rows with value "venues" for column
source will come from the table
my_venues ,
rows with value "programmes" for
column source will come from table
my_programmes.
I am trying to perform search on multiple tables.
I will simplify problem and say that I have 2 tables Worker and Customer both have Id, Name, Surname and Worker has additional Position, all fields are varchar except Id which is Int.
How to make a query that will return rows of either Customer or Worker, where one of theirs fields contains entered search string.
I have tried with joins but I got returned joined row also.
select id,name,surname,position,'worker' as tbl from worker where ..
union all
select id,name,surname,'','customer' from customer where ...
In this way you can even know results what table belong to.
Just UNION both queries.
If you really can JOIN those two, you can use
an IF statement in the SELECT clause to show the right field.
But, from what I understand from your question, go with UNION