SELECT Multiple Tables - php

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?

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

How to select two additional columns from another table based on id in the main table?

How to select two additional columns from another table based on id in the main table?
SELECT t1.*, t2.*
FROM tbl_setup t1, tbl_specialty t2
WHERE t1.app_id = 12
Use a JOIN:
SELECT * FROM tbl_setup t1
JOIN tbl_speciality t2
ON t1.app_idd = t2.t1_app_id
I am not sure, but I don't think you can use "$" in a MySQL column name without running into problems later.
A nice explanation of MySQL Joins:
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

How to select * from multiple table

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.

nested mysql query with double select statement?

I have a simple problem regarding a nested query.
To be honest I don't know if it can be done through one query only or if I'll have to use PHP.
Put simply, I want to return the users information from the users tables by the users IDs returned from a select statement in relations table.
I can do this by 2 queries and some PHP loop but for saving resources, but I think it's better to combine it into 1 query and single loop.
First query
SELECT UserID FROM relations WHERE GroupID = '1'
Second query I need to retrieve the user info from the user table by the returned UsersIDs from the first select statement.
I can do this by loop through the ID and making the queries but I think I can get all in 1 query.
Thanks
This is the typical way to do that:
SELECT users.*
FROM users
INNER JOIN relations
ON users.id = relations.userid
WHERE relations.groupid = 1
I noticed you were using quotes around the 1 in your query. I am assuming group id is an integer value, in which case you should not use quotes.
The answers that use an IN subquery are likely to be less performant. Esp. with MySQL, the JOIN should always we the preferred way to combine results from tables, since MySQL has particularly lackluster subquery implementation.
Select * from user_table where id in(SELECT UserID FROM relations WHERE GroupID = '1')
OR
Select * from user_table u INNER JOIN relations r ON u.UserID=r.UserID WHERE r.GroupID='1'
select * from user_typw
where userID in (SELECT UserID FROM relations WHERE GroupID = '1')
try this
select * from user where UserID in
(SELECT UserID FROM relations WHERE GroupID = '1')
or
select * from user U where exists
(SELECT * FROM relations R WHERE U.UserID=R.UserID and R.GroupID = '1')
or
select U.*
from user U join relations R
on U.UserID=R.UserID
where R.GroupID = '1'
SELECT * FROM user_table ut LEFT JOIN relations r on ut.UserID=r.UserID where r.GroupID=1;
SELECT A.field1,B.field2 FROM table1 A LEFT JOIN table2 B ON A.common_ID=B.common_ID WHERE field = 'abc'

How to link Id from one table with another

i have spent the last 2 days going over this problem trying to work it out.
i have two tables
one is the user_login and it has a row called user_id the other is agedcare and it has a row called id
i have tried to get the 2 to link together
i want to be able to update the agedcare using the id from user_login as the main id
$query = "SELECT id FROM agedcare INNER JOIN user_id ON login_users = login_users.user_id WHERE login_users.user_id = '$id'"
The format is something more like..
SELECT id,name,age
FROM Table1
INNER JOIN Table2
ON Table1.Field=Table2.Field
Another way to simplify the select statement (so that you avoid using the table names throughout) is to "rename" the table right after you name it. For example, the line that has "FROM Table1 T1" - the T1 immediately after the table name is "renaming" the table allowing you to shorten the table name when you need to refer to it. In addition, you can use the t1 and t2 identifiers when selecting the fields.
SELECT t1.id,t1.name,t2.age
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.Field=T2.Field
Check your format of join, it must be like this
SELECT * FROM t1 as a LEFT JOIN t2 as b ON a.id=b.id where user_id = 'id'

Categories