LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2011, 01:42 PM   #1
struct
Member
 
Registered: Feb 2009
Posts: 68

Rep: Reputation: 10
SQL Alias problem... point out my error please.


Hi Everyone,
I'm having some difficulties understanding why this isn't working. why would some alias work and some doesn't? Could someone point out what I'm doing wrong?
While not using Alias for t1.PARENT and t2.PROJECT_ID, it works without any errors.

without alias for t1.PARENT & t2.PROJECT_ID
Code:
SELECT t1.PARENT, t2.PROJECT_ID, t2.PROJECT_NAME AS PARENT_NAME
FROM TABLE1 t1 INNER JOIN TABLE1 t2
ON t1.PARENT = t2.PROJECT_ID
ORDER BY PARENT_NAME ASC
using alias PARENTID & PROJECTID
Code:
SELECT t1.PARENT AS PARENTID, t2.PROJECT_ID AS PROJECTID, t2.PROJECT_NAME AS PARENT_NAME
FROM TABLE1 t1 INNER JOIN TABLE1 t2
ON PARENTID = PROJECTID
ORDER BY PARENT_NAME ASC
causes
Code:
ERROR: An undefined column or parameter name was detected.

Last edited by struct; 01-17-2011 at 02:21 PM. Reason: edited to make it easier to understand.
 
Old 01-17-2011, 02:52 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

This might make it clearer (mySQL 4.1 or higher, MSSQL 7.0, Oracle 8i or higher):
Code:
select c1.center_name, c2.address_city from
    -> centers c1 inner join center_addresses c2
    -> on c1.center_id=c2.center_id;
+-------------------------------------+----------------------+
| center_name                         | address_city         |
+-------------------------------------+----------------------+
| 300 Bowl                            | Albany               |
| 300 Bowl                            | Medford              |
| XYZ   Lanes                         | Waterford            |
...
Code:
select c1.center_name as 'Customer', c2.address_city as 'City'
    ->   from centers c1 inner join center_addresses c2
    ->   on c1.center_id=c2.center_id;
+-------------------------------------+----------------------+
| Customer                            | City                 |
+-------------------------------------+----------------------+
| 300 Bowl                            | Albany               |
| 300 Bowl                            | Medford              |
| XYZ   Lanes                         | Waterford            |
...
PS:
You can get rid of the syntax error simply by quoting 'ProjectID', 'ParentID', etc. I'm just not sure that's what you were looking for

Last edited by paulsm4; 01-17-2011 at 02:55 PM.
 
Old 01-17-2011, 03:04 PM   #3
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 186

Rep: Reputation: 25
Post

Well, I believe this is no standard SQL, so it depends on your database server (you didn't
say which...). I believe PostgreSQL can do that, MS SQL Server can't...
Cheers, Tom...
 
Old 01-17-2011, 03:15 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Standard SQL doesn't allow for an alias that aliases both table & column.
You can do EITHER, but not BOTH.

Last edited by Tinkster; 01-17-2011 at 03:18 PM.
 
Old 01-17-2011, 03:31 PM   #5
struct
Member
 
Registered: Feb 2009
Posts: 68

Original Poster
Rep: Reputation: 10
Paulsm4,
thank you for taking your time to illustrate that. Unfortunately putting a single quote around the alias causes same error in my system. Your method worked because you joined the columns with tableAlias.column name instead of column Alias.


Cheesus,
I'm on DB2.


Tinkster,
Your answer is sound but ORDER BY PARENT_NAME works on my system. So... I don't think your answer applies here.

Anyways, I decided to go with Paulsm4's method. Thank you for giving me your valuable input.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Point my.cnf to the new sql path ASTRAPI Linux - Server 13 06-13-2010 04:11 PM
php / MySQL: You have an error in your SQL syntax error FirstBorn Programming 6 12-31-2008 05:46 PM
how to create an alias (apache) to point to a cd-rom/dvd verlossen Linux - Networking 1 11-20-2005 03:50 PM
Apache alias point to mounted windows share johnnypezz Linux - Newbie 0 09-13-2004 02:04 PM
SQL Startup error : Error while setting value '@MYSQL_TCP_PORT@' to 'port' glenn69 Linux - Newbie 1 08-07-2004 11:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration