Solved

join mysql with ambiguous field name

Posted on 2013-07-01
7
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate PDF from MySQL using PHP 3 71
PowerShell and cisco ios 3 69
Cannot exit IF statement in PowerShell 4 47
Add Log to powershell Script 14 30
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

752 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