Solved

join mysql with ambiguous field name

Posted on 2013-07-01
7
535 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Logon script fails 23 45
Using cfstoredproc to return query data 2 30
AWK Code gives empty file, why? 12 36
Uploading a CSV Data Import via PHP & MySql 3 29
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

807 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