How to select * from multiple table - php

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.

Related

How to select row from two different tables using specific id in mysql?

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';

get all record without join in sql

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

SELECT Multiple Tables

I'm stuck in a piece of code that does not quite understand.
I have several tables with different names but same fields, but the tables are independent
Something like that:
table1
id
user
title
table2
id
user
title
I need to get in the same query data from two tables but I fail, I try with INNER JOIN, UNION ALL, but not knowing, it misapplied.
Right now I have this:
$mysites = $db->QueryFetchArrayAll("
select *
FROM table1,table2
where table1.user = table2.user AND
table1.user = 1");
foreach($mysites as $mysite){
echo $QUERY['title'];
}
but returned this:
title1.table1
title2.table1
and i like this:
title1.table1
title2.table1
title1.table2
title2.table2
A greeting and thanks
You can use the keyword UNION like this:
SELECT * FROM table1 UNION SELECT * FROM table2
This query will select everything from table1 and merge the results with those from table2. Please note that you have to select the same number of columns from both tables. Moreover, column names and datatypes will be assigned according to first table.
If you want to preserve duplicates add the keyword ALL:
SELECT * FROM table1 UNION ALL SELECT * FROM table2
The question is very unclear.....
Are the ID's the same in each table for each user? If so an INNERJOIN will help
SELECT t1.*, t2.*
FROM table1.t1
INNER JOIN table2.t2
ON t1.id = t2.id
WHERE t1.user = "1"
(Change INNER JOIN to LEFT JOIN if the data could be missing)
If this is not the case, why not put the data from one table into the other, and have just one table with all the data in it?

MSSQL Joining 3 Tables with more than 1 row to a single line result over multiple records

Ok, I'll try to explain this as best I can here.
I have multiple tables that are to be connected through a JOIN where certain reference points meet.
In one of the tables, there are 2 or more results over several rows that I need to bring back to separate columns.
In the diagram below (I hope that explains it better), T2.ColA is connected to T3.ColA and T1,ColA is connected to T2.ColB.
In ColC of T1, there is a latest record. These are the only ones that are required to results. Note that ColC could be different dates between rows 1 and 2 for example. But it needs the latest for each ColB based on ColA.
But in T1, there are two rows which need Col B to return to the result in separate columns.
By the way, this is just one entry - there will be thousands of rows that need to return a result - not just 1.
Let me know if you need any more info.
Try this query
select t1.cola,t2.cola,t1.colb,t2.colb,t3.colb FROM table2 t2 INNER JOIN table1 t1 ON t1.cola = t2.colb INNER JOIN table3 ON t3.cola =t2.cola
Possibly this could solve your query..
SELECT T1.COLA, T1.COLB, T2.COLA, T2.COLA
FROM
TABLE1 T1 INNER JOIN TABLE2 T2
INNER JOIN TABLE3 T3
WHERE
T1.COLC = (
SELECT MAX(T4.COLC) FROM TABLE1 T4 WHERE T4.COLA = T1.COLA
)
** But as you have specified in your dig, it's not possible to display 1123, 3211 in a single row. It has to be in two different rows, coz its changes dynamically depending on the row count. You can modify as you want using your front end application.

mysql query to select data from table2 which does not exist in table1

I have 2 tables table1 and table2, where table1 contains the data collected so far, and table2 contains table1's data along with some updated/additional data. I need to retrieve only this updated/additional data and insert it into table1.
Now I know I can use NOT IN to do this, but I am not sure it will be a very efficient solution in case of a huge number of records. Any suggestion on what the best approach would be in terms of execution speed?
This can be done with simple join both tables
something like below:
select t1.* from table1 as t1 join table2 as t2 on t1.id=t2.id where ...[]
I'm not sure if i've understand your question correctly but let me give it a try. Suppose you have a design like this:
TableA : {colA, colB, colC, colD, colE}
TableB : {colA, colB, RecC, RecD, RecE}
where Tables (tableA, tableB) is joined on ColA. I Assumed that TableA's columns (colC, ColD, colE) will be updated and the records are based on TableB's columns (recC, recD, recE).
In your statement: I need to retrieve only this updated/additional data and insert it into table1.. I think you want to update TableA's records based on TableB
UPDATE TableA a INNER JOIN TableB b ON a.ColA = b.ColA
SET a.ColC = b.RecC,
a.ColD = b.RecD,
a.ColE = b.RecE
-- WHERE (Condition) -- if you want to have a condition.
so the statement above updates all the records in tableA if colA also exist in tableB since I've used INNER JOIN. You could also use LEFT JOIN.

Categories