473,667 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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..20 040.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 6326

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_FRO M 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_FRO M 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***********@a qsacom.com
tél : 01 69 29 36 18
----------------------------------------------------------------------------
---------------

-----Original Message-----
From: pg************* ****@postgresql .org
[mailto:pg****** ***********@pos tgresql.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..20 040.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
3062
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 solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche Nachricht-----
2
1591
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
2077
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 tblUniqueSups by doing a select Distinct Supervisor Name. Now I need to bring in the SSNs of the Unique Sups but I can't quite get it. I tried: UPDATE UniqueSups LEFT JOIN tblNonNormalized ON UniqueSups.NAME = tblNonNormalized.SupervisorName SET...
2
2730
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, FNameBirth, DOB, 'Birth' from tblBirth UNION SELECT SurnameAlias, FNameAlias, #1/1/100#, 'Alias' from tblAlias; What I am doing here is searching two tables for a person where the name given may be the person's original name (at birth) or subsequent...
0
7905
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 between then and now I learned how to return "setof" values from a function, as well as how to construct "dynamic" queries inside a function. Returning the top 10 values from a query is no big deal: create table my_table (field1 integer,...
6
2236
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 statement that stay behind a current, but not yet saved query. When I work on saved queries I use: strCurrentName = CurrentObjectName Dim dbsCurrent As Database Set dbsCurrent = CurrentDb
8
3071
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 position. i can select the row i want to delete, but don't know how to delete just this row. here's my select:
10
2578
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 tool you know how to use is a hammer, every problem tends to look like a nail. That said, I could solve my problem in C, but it's not the right tool. I need to come into the Windows world, and I need to get this done in Access or something...
5
2547
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 customers linked to a table of customer contacts. The contact table has a field called 'type of contact'. Contact types can be numeric 1 to 40 and show the different types of contact I have with my customers (e.g. 1 - initial contact, 2 -...
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.