Solved

join mysql with ambiguous field name

Posted on 2013-07-01
7
492 Views
Last Modified: 2013-07-01
table name:  TableA
TableA cols:  aID, aName,image
table name:  TableB
TableB cols:  bID, bName,image

select * from TableA a left join TableB b on a.aID=b.bID where a.status=1
this returns the full list  (both 'image' columns)

however, I want to be able to access the image column on table A

I think it I need to refer to it as TableA.image as getImage
however I don't know the exact syntax.

pls help
0
Comment
Question by:joomla
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39289537
don't use "*"

select ... tableA.image as tabAimage ...
0
 
LVL 9

Assisted Solution

by:TvMpt
TvMpt earned 75 total points
ID: 39289539
You can set aliases for the columns that you are selecting:

$query = 'SELECT news.id AS newsId, user.id AS userId, [OTHER FIELDS HERE] FROM news JOIN users ON news.user = user.id'

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 175 total points
ID: 39289545
in a more complete form:
select a.aID, a.aName, a.image as a_image, b.bID, b.bName, b.image as b_image
from TableA a 
left join TableB b on a.aID=b.bID 
where a.status=1

Open in new window

spell out the selection list, use the table alias on each field, and if there is a common field name, provide a field alias to help differentiate them
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:joomla
ID: 39289553
Hi and thanks to both for the feedback
In the live scenario, rather than the example provided, both tables have many columns.
Can you provide me with a wildcard option
thanks
0
 
LVL 9

Accepted Solution

by:
TvMpt earned 75 total points
ID: 39289565
It's probably a bad practice but it worked for me in this case.

You can select all of the columns from a specific table by using table_name.* in your select statement.

When you have duplicated column names, mysql will overwrite from first to last. The data from the first duplicated column name will be overwritten when it encounters that column name again. So the duplicate column name that comes in last wins.

If I am joining 3 tables, each containing a duplicated column name, the order of the tables in the select statement will determine what data I am getting for the duplicate column.

Example:

SELECT table1.* , table2.* , table3.* FROM table1 LEFT JOIN table2 ON table1.dup = table2.dup LEFT JOIN table3 ON table2.dup = table3.dup;

Open in new window

In the example above, the value of dup I get will be from table3.

What if I want dup to be the value from table1?

Then I need to do this:

SELECT table3.* , table2.* , table1.* FROM table1 LEFT JOIN table2 ON table1.dup = table2.dup LEFT JOIN table3 ON table2.dup = table3.dup;

Open in new window


Now, table1 comes last, so the value of dup will be the value from table1.

I got the value I wanted for dup without having to write out every single freaking column and I still get all of the columns to work with. Yay!

I know the value of dup should be the same in all 3 tables, but what if table3 doesn't have a matching value for dup? Then dup would be blank in the first example, and that would be a bummer.
0
 

Author Comment

by:joomla
ID: 39289573
Hi TvMpt,
I tried your solution but struggled to with the third table and generated errors when I tried it.
In this instance I'm going to move forward and explicitly reference each column.

I'm going to offer 75% of points to you for your additional efforts, 25% to the other expert who offered similar advice

hope this is acceptable
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39289586
it is simply bad practice to use "*" in SQL (regardless of dbms)

but as the prior comment notes, you may use the table (or table alias) with wildcard to help narrow the problem down.

however you may still have the potential issue of common field names, and you need more than one of them.

best practice is to literally specify the fields you need.

Note, let's say each table has 30 fields.

table1.* , table2.* , table3.* => 90 fields

if you only require 20 fields in total, that;s 70 fields you have gathered you will simply dispose of. This is inefficient in the dbms, the network, and our 'consuming program'.

Note, when ou do "select *" or "select tableX.*", the dbms also has to go an figure out what qualifies for the "*" (i,e, like any wildcard searcah there's an overhead.

spell out the selection list,
use the table alias on each field,
and if there is a common field name, provide a field alias to help differentiate them

i was adding this as you were accepting
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now