Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

join mysql with ambiguous field name

Posted on 2013-07-01
7
Medium Priority
?
622 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 49

Expert Comment

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

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

Assisted Solution

by:TvMpt
TvMpt earned 300 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 700 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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 300 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 49

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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

661 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