i have two tables table1 and table2, table1 in 10 filed available and tbale2 in 6 filed available.but no any relation between them.
i want to get all record from both table.
Use cross join
Select t.*,t1.* from table t cross join table1 t1
If you want all the records in the same table use the above query it will join and give m*n rows where m and n are number of rows in the tables
You can use union all if you want all the results added m+n number of results
Select * from table
Union all
Select * from table1
You need to specify the columns if you need specific columns from both tables. Or if you have different number of columns in the tables
If you have at least some common columns, you can union them together. For example:
Table1
Name Description Quantity Price
Table2
Name Description OrderDate Blah BlahBlah
You can do something like this:
SELECT Name, Description FROM Table1
UNION ALL
SELECT Name, Description FROM Table2
That would give you a result set with 2 columns (Name, Description, OrderDate) that is made up of rows from both Table1 and Table2
Related
I have 3 different tables in db those are like tbl1, tbl2, tbl3
in these all table have one same columns 'direction'.
tbl1 contain one value either tbl2 or tbl3,
tbl2 contains one value tbl3.
How can I select the rows from tbl1 and tbl2 which contains direction like equal tbl3.
i also include my two table image in this image 2 columns name is same level_redir now i want which row that have value like hardening
enter image description hereenter image description here
please suggest me which query is good for getting row from two different tables in which table columns where dir_redirect = 'hardening'.
Since your question is not clear and you have not included all the table structure its bit difficult to figure out.
Let me help you with generic way
The following query we have joined all the 3 tables based on the common tables with are present in the respective tables. The following query select all the columns from all the 3 tables.
SELECT * FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
You can select from specific tables by the following
SELECT t1.col1, t1.col2, t2.col1, t2.col2 FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
actually I have two tables ,i.e table 1 and table 2 . I need to display data from both tables . if I use
SELECT * FROM table 1 UNION SELECT * FROM table 2
Now I am getting data randomly from both tables
BUT I need first display table1 data after display table2 data
In practice, this will do what you want (assuming the tables have the same columns):
SELECT * FROM table1
UNION ALL
SELECT * FROM table2;
It is actually safer to use an explicit order by:
SELECT . . .
FROM (SELECT t1.*, 1 as which FROM table1 t1
UNION ALL
SELECT t2.*, 2 as which FROM table2 t2
) t
ORDER BY which;
SQL tables represent unordered sets. There is no ANSI requirement that UNION ALL return values from the first subquery before the second. In practice that works in MySQL.
One way could be adding a column in the both queries which would indicate the source from where it came.
SELECT
*
FROM
(
SELECT *,'T1' AS source FROM table1
UNION
SELECT *,'T2' FROM table2
) AS t
ORDER BY t.source ASC;
See Demo
I have two tables, table1 and table2. table1 has 2 rows. table2 has 3 rows. so, totally table1 and table2 have 5 rows. I want to show the 5 rows by selecting table1 and table2 at a time. how todo? can you help me please. don't add any where clause.
depends on what the table structure is. if you have a PK > FK relationship you can join the tables like so
SELECT stuff
FROM table1 t1
JOIN table2 t2 ON t1.someID = t2.someID
if there is no correlation then you can use a UNION
SELECT stuff
FROM table1
UNION
SELECT stuff
FROM table2
One thing to note about using a UNION. the columns have to match so if you have the same type of data in both tables this works fine or else you will have to specify which columns to be selected out.
I want to merge the value records as single row based on ID value.I want to display table1 value along with table2 value as single row. I want to display all in single record for the id=15.In table 1 ID is sub_id in table 2.
I want output as
Id content_value value as(15,cake,chocolate,enila,strabery)
select a.*,b.CONTENT_VALUE as multitype from album as a,album_details as b where a.ID=b.SUB_ID
Not possible to get as different columns for each value or content_value since the number of rows is dynamic.You can get the values like this by joining two tables. ie as two columns (ID and all content_value & value).
SELECT t1.ID, CONCAT_WS(',', t1.content_value, GROUP_CONCAT(t2.value)) AS contents FROM table1 t1
INNER JOIN table2 t2
ON t2.sub_id = t1.ID
GROUP BY t1.ID;
There are 4 tables table1,table2,table3 and table4
table1 has got 60000 datas
table2 has got 85000 datas
table3 has got 78000 datas
table4 has got 68000 datas
indexes on all tables are same but one got less than or more than each other. for example name john is stored in all 4 tables.but mathew is stored in may be two tables but not in other two and ethan may be stored in 3 tables but not may be in 4th one.
upto say first 60k all index/names are same but after that it is irregular
so how can I merge all these table into 1 table?? all four tables got 2 columns each and first one is name and second is its details
There are probably more efficient ways of doing this, but this was the first thing that came to mind.
INSERT INTO table5
SELECT DISTINCT Table5Content.* FROM (
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
UNION ALL
SELECT * FROM table4
UNION ALL
SELECT * FROM table5
) as Table5Content