I need to export the data from several tables into a .csv file. I've written the CSV output code but now I need to structure the query properly.
Each table contains a USER_ID
field. This field is used to relate the user's data from one table to another. I need to grab all the data from these tables, with each user's information fully represented on a single row.
simple example of what I need:
TABLE A
|USER ID | NAME | FAVORITE_COLOR | AGE |
|007 |James | black | 46 |
----------------------------------------
TABLE B
|USER ID | JOB | LOCATION | NOTES |
|007 |Agent| N/A | cool guy |
--------------------------------------
JOINED OUTPUT I NEED (albeit with many more users / tables):
|USER ID | NAME | FAVORITE_COLOR | AGE | JOB | LOCATION | NOTES |
|007 |James | black | 46 |Agent| N/A | cool guy |
--------------------------------------------------------------------
I know this can be accomplished with joins
but I'm not sure how to make a query which selects ALL the users, and joins ALL their data from several tables, so that each user is a single row with all their data.
Any help or a code nudge in the right direction is appreciated! :)
试试这个查询
select t1.*,t2.* from table_1 as t1 left join table_2 as t2 on t1.user_id = t2.user_id
try this
select t1.USER ID , NAME , FAVORITE_COLOR , AGE ,JOB , LOCATION , NOTES
from TABLE1 t1
INNER JOIN table2 t2
ON t1.USER_ID = t2.USER_ID
EDIT>
if you have 3 or 4 tables then just join them you can add this
INNER JOIN table3 t3
ON t2.USER_ID = t3.USER_ID
INNER JOIN table4 t4
ON t3.USER_ID = t4.USER_ID
With the following tables
TABLE A
|USER ID | NAME | FAVORITE_COLOR | AGE |
|007 |James | black | 46 |
|008 |John | Doe | 77 |
|010 |Amy | McClain | 26 |
----------------------------------------
TABLE B
|USER ID | JOB | LOCATION | NOTES |
|007 |Agent| N/A | cool guy |
|010 |Agent| MI | babe |
--------------------------------------
SELECT TableA.*, TableB.*
FROM TableA
LEFT JOIN TableB
ON TableA.[USER ID] = TableB.[USER ID]
Will will pull all records found in TableA
regardless of if there is a matching record in TableB
. Here is what the results would look like:
|USER ID | NAME | FAVORITE_COLOR | AGE | JOB | LOCATION | NOTES |
|007 |James | black | 46 |Agent| N/A | cool guy |
|008 |John | Doe | 77 |Null | Null | Null |
|010 |Amy | McClain | 26 |Agent| MI | babe |
By changing the LEFT JOIN
to a INNER JOIN
you can force the query to pull only records that are found in both tables. Here is what the results would look like:
|USER ID | NAME | FAVORITE_COLOR | AGE | JOB | LOCATION | NOTES |
|007 |James | black | 46 |Agent| N/A | cool guy |
|010 |Amy | McClain | 26 |Agent| MI | babe |
试试这个查询
$qry = "SELECT t1.USER_ID,t1.NAME,t1.FAVORITE_COLOR,t1.AGE,t2.JOB,t2.LOCATION,t2.NOTES FROM TABLE_1 as t1 LEFT JOIN TABLE_2 as t2 ON t1.USER_ID = t2.USER_ID";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.