473,394 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

query with table alias

Hello,

I think I have found a query problem when the query has an alias for a table
and use alias item and table name.

I ilustrate the problem with a simple table and query.

prr=# create table foo (c1 int2, c2 int2);
CREATE TABLE
prr=# insert into foo values (1, 1);
INSERT 301891 1
prr=# insert into foo values (2, 2);
INSERT 301892 1
prr=# select * from foo;
c1 | c2
----+----
1 | 1
2 | 2
(2 filas)

prr=# select foo.c1, f.c2 from foo f; -- >>> Incorrect result <<<
c1 | c2
----+----
1 | 1
2 | 1
1 | 2
2 | 2
(4 filas)

prr=# explain select foo.c1, f.c2 from foo f; -- >>> Incorrect query plan
<<<
QUERY PLAN
-------------------------------------------------------------------
Nested Loop (cost=20.00..20040.00 rows=1000000 width=4)
-> Seq Scan on foo f (cost=0.00..20.00 rows=1000 width=2)
-> Materialize (cost=20.00..30.00 rows=1000 width=2)
-> Seq Scan on foo (cost=0.00..20.00 rows=1000 width=2)
(4 filas)

The result of the "select foo.c1, f.c2 from foo f" isn't correct, it do a
cartesian product of foo table.

My version of PostgreSQL is PostgreSQL 7.4.5 on i386-pc-linux-gnu, compiled
by GCC i386-linux-gcc (GCC) 3.3.4 (Debian 1:3.3.4-9).

Is this a bug?, is fixed in posteriors versions?

Thanks in advance,

Nov 23 '05 #1
3 6299

On Nov 12, 2004, at 7:48 PM, Rodríguez Rodríguez, Pere wrote:
prr=# select foo.c1, f.c2 from foo f;* -- >>> Incorrect result <<<
*c1 | c2
----+----
* 1 |* 1
* 2 |* 1
* 1 |* 2
* 2 |* 2
(4 filas)
If you alias a table, you can only reference the table using the alias.
It is in effect renamed: foo is no longer foo. It is only f after you
alias foo to f.
The result of the "select foo.c1, f.c2 from foo f" isn't correct, it
do a cartesian product of foo table.


When you do reference foo, PostgreSQL adds foo to the FROM list if you
have the ADD_MISSING_FROM configuration parameter set to TRUE in
postgresql.conf.

Thus, PostgreSQL considers your query to be
SELECT foo.c1, f.c2 FROM foo f, foo;

which results in the Cartesian join your are seeing.

Some people like this, some people don't. (I'm one of the latter.) If
you would rather PostgreSQL throws an error in this situation, set
ADD_MISSING_FROM to FALSE.

Hope this helps.

Michael Glaesemann
grzm myrealbox com
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
The correct query would be "select f.c1, f.c2 from foo f;" if you want to
use alias....

Patrick
---------------------------------------------------------------------------
----------------
Patrick Fiche
email : pa***********@aqsacom.com
tél : 01 69 29 36 18
----------------------------------------------------------------------------
---------------

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org]On Behalf Of Rodríguez Rodríguez,
Pere
Sent: vendredi 12 novembre 2004 11:49
To: 'p************@postgresql.org'
Subject: [GENERAL] query with table alias

Hello,

I think I have found a query problem when the query has an alias for a table
and use alias item and table name.

I ilustrate the problem with a simple table and query.

prr=# create table foo (c1 int2, c2 int2);
CREATE TABLE
prr=# insert into foo values (1, 1);
INSERT 301891 1
prr=# insert into foo values (2, 2);
INSERT 301892 1
prr=# select * from foo;
c1 | c2
----+----
1 | 1
2 | 2
(2 filas)

prr=# select foo.c1, f.c2 from foo f; -- >>> Incorrect result <<<
c1 | c2
----+----
1 | 1
2 | 1
1 | 2
2 | 2
(4 filas)

prr=# explain select foo.c1, f.c2 from foo f; -- >>> Incorrect query plan
<<<
QUERY PLAN
-------------------------------------------------------------------
Nested Loop (cost=20.00..20040.00 rows=1000000 width=4)
-> Seq Scan on foo f (cost=0.00..20.00 rows=1000 width=2)
-> Materialize (cost=20.00..30.00 rows=1000 width=2)
-> Seq Scan on foo (cost=0.00..20.00 rows=1000 width=2)
(4 filas)

The result of the "select foo.c1, f.c2 from foo f" isn't correct, it do a
cartesian product of foo table.

My version of PostgreSQL is PostgreSQL 7.4.5 on i386-pc-linux-gnu, compiled
by GCC i386-linux-gcc (GCC) 3.3.4 (Debian 1:3.3.4-9).

Is this a bug?, is fixed in posteriors versions?

Thanks in advance,


Protected by Polesoft Lockspam
http://www.polesoft.com/refer.html

Nov 23 '05 #3
prr=# select foo.c1, f.c2 from foo f; -- >>> Incorrect result <<<

The result of the "select foo.c1, f.c2 from foo f" isn't correct, it do
a cartesian product of foo table.


foo is aliased to f, so there's no table named foo in the from clause.
By default postgres tries to add the missing table name, so your query
get rewritten as:

select foo.c1, f.c2 from foo f, foo;

You should also receive a:

NOTICE: adding missing FROM-clause entry for table "foo"
Best regards
--
Matteo Beccati
http://phpadsnew.com
http://phppgads.com
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
2
by: muzamil | last post by:
Hi Your help for the following query will be highly apprecaited. I've wasted alot of time on it. Data definition is at the bottom. Thanks -----------------------------------
6
by: mo | last post by:
I need to bring the ssn's into UniqueSups (supervisors) from tblNonNormalized. My inherited DB is not normalized and I find it extremely irritating due to the workarounds needed. I created...
2
by: Lyn | last post by:
Hi, How do you bind the output columns from a UNION query when the fields from the two tables have different names? Consider this query (WHERE clauses omitted)... SELECT SurnameBirth,...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
6
by: sghi | last post by:
Hi All, I'm new to this group and quite new to access/vba. So, shortly after beginning to write a simple application for my wife, I came across a blocking problem: I need to intercept the sql...
8
by: starman7 | last post by:
i have a table with objects in categories and their positions. there will be several rows with category 400, and they will have various positions, i want to delete only the row with the lowest...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
5
by: redstamp | last post by:
Try as I might I cannot find a way to write an access query to return a result set with the records from my database WITHOUT a certain set of values within a field. To explain, I have a table of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.