Gags
Gags

Reputation: 3829

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

enter image description here

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.

Upvotes: 0

Views: 948

Answers (2)

AdamMc331
AdamMc331

Reputation: 16691

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 = ?

Upvotes: 1

akr
akr

Reputation: 739

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 = ?

Upvotes: 0

Related Questions