tables with same field name

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

tables with same field name

Post by jayson.ph »

Hi all,

i have, a 3 tables with different data inside but same field name. and now it is possible to join 3 table to show up in webpage. if it is possible what should sql statement i use.
Last edited by jayson.ph on Thu Jul 05, 2012 10:35 pm, edited 2 times in total.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: tables with same field name

Post by jayson.ph »

ex.

table1
field-a = ibm
field-b = asus
field-c = apple

table2
field-a = samsung
field-b = lenovo
field-c = hp

page->

field-a ==========field-b=========field-c
ibm=============asus===========apple
samsung=========lenovo========= hp
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: tables with same field name

Post by Christopher »

Use the table names to specify the field names and use AS to rename fields.

SELECT table1.field-a AS t1fa, table2.field-a AS t2fa FROM table1 JOIN table2 ON table1.field-a=table2.field-a
(#10850)
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: tables with same field name[Solve]

Post by jayson.ph »

Thank 'Christopher' i gut it. thank..
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: tables with same field name[SOLVE]

Post by jayson.ph »

Hi all, another question, this situation is i use array to show up the following data. i wondering since i have two same name field. what should the name i put inside the array. between two table1.field-a AS t1fa, table2.field-a AS t2fa.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: tables with same field name

Post by jayson.ph »

Please help to construct:

Code: Select all

SELECT tbl_update_request.id AS myid,
						tbl_update_request.uid AS myuid,
						tbl_update_request.cat_name AS mycat_name,
						tbl_update_request.product_name AS myproduct_name,
						tbl_update_request.product_model AS myproduct_model,
						tbl_update_request.product_desc AS myproduct_desc,
						tbl_update_request.re_qty AS myre_qty,
						tbl_update_request.dt_nwo AS mydt_nwo,
						tbl_update_request.commnt AS mycommnt,
						tbl_update_request.rec_on AS myrec_on,
						tbl_update_request.return_on AS myreturn_on,
						tbl_update_request.balancs AS mybalancs,
						tbl_update_request.sold AS mysold,
						tbl_update_request.tbl_brnch_name AS mytbl_brnch_name
						FROM (tbl_update_request.uid,
						tbl_update_request.cat_name,
						tbl_update_request.product_name,
						tbl_update_request.product_model,
						tbl_update_request.product_desc,
						tbl_update_request.re_qty,
						tbl_update_request.dt_nwo,
						tbl_update_request.commnt,
						tbl_update_request.rec_on,
						tbl_update_request.return_on,
						tbl_update_request.balancs,
						tbl_update_request.sold,
						tbl_update_request.tbl_brnch_name FROM tbl_update_request UNION ALL SELECT
						tbl_brn_requst.id,
						tbl_brn_requst.cat_name,
						tbl_brn_requst.product_name,
						tbl_brn_requst.product_model,
						tbl_brn_requst.rec_on,
						tbl_brn_requst.return_on,
						tbl_brn_requst.balancs,
						tbl_brn_requst.sold,
						tbl_brn_requst.re_qty,
						tbl_brn_requst.dt_nwo,
						tbl_brn_requst.commnt FROM tbl_brn_requst UNION ALL SELECT tbl_user.uid,
						tbl_user.tbl_brnch_name FROM tbl_user)";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: tables with same field name[SOLVE]

Post by Christopher »

jayson.ph wrote:Hi all, another question, this situation is i use array to show up the following data. i wondering since i have two same name field. what should the name i put inside the array. between two table1.field-a AS t1fa, table2.field-a AS t2fa.
You will get:

Code: Select all

array(
't1fa' => 'foo',
't2fa' => 'bar',
);
(#10850)
Post Reply