Solved

join mysql with ambiguous field name

Posted on 2013-07-01
7
516 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

770 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