简体   繁体   中英

Fetch Data from two tables mySQL PHP

I have two tables in my database. "A" table is the main table that stores user primary information and other table "B" stores if user wants to add some additional address onto their profile.

Structure of both tables( has common columns with exact same names ) is as per below picture except some diff columns that are not shown in this pic

在此处输入图片说明

Now i want to display Address stored in Table A as well as in Table B

I used below queries but all these return only values from Table B

Query 1 :

Select t1.(star),t2.(star) from `b` t2 , `a` t1 WHERE t1.emailbc = ?; 

Query 2 :

Select t1.(star),t2.(star) from `a` t1 
INNER JOIN `b` t2 ON (a.emailbc=b.emailbc) 
WHERE t1.emailbc = ?

I also tried NATURAL Join but that does not work either. Please let me know solution.

If you want to display all addresses in one column, but coming from both tables, you need to use a UNION . Try this:

SELECT *
FROM table1
WHERE emailbc = ?
UNION
SELECT *
FROM table2
WHERE emailbc = ?

Change your join query to the following.

Select t1.*,t2.* from `a` t1 
INNER JOIN `b` t2 ON (t1.emailbc=t2.emailbc) 
WHERE t1.emailbc = ?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM