How does one embed a sub-query lookup to one table in order to
replace a foreign key id number with it's name in a SELECT on a
second table?
i.e.: given the following two tables, I want to replace the color_id
of 1
with the color_name 'red.' (The SQL to create the two tables follows
below.)
test=# SELECT * from users ;
color_id | name | the_date
----------+------+------------
1 | john | 2004-03-10
3 | jane | 2004-04-12
1 | joe | 2004-05-14
2 | jepe | 2004-06-16
(4 rows)
test=# SELECT * from colors;
color_id | color_name
----------+------------
1 | red
2 | green
3 | blue
(3 rows)
My attempts yield an 'f' which looks suspiciously like a boolean false.
Is there an ordering issue with my sub-query, such that the sub-query
doesn't have enough info to perform its lookup?
Here's my query:
SELECT (
u.color_id = (
SELECT c.color_name
FROM colors c
WHERE color_id = 1)) AS color_name,
u.name, u.the_date
FROM users u
WHERE u.color_id = 1
ORDER BY u.the_date DESC LIMIT 1;
It returns:
color_name | name | the_date
------------+------+------------
f | joe | 2004-05-14
(1 row)
Thanks!
Scott
Here's the SQL to create my test tables:
CREATE TABLE colors (color_id SERIAL PRIMARY KEY, color_name text);
CREATE TABLE users (color_id integer REFERENCES colors, name text,
the_date date);
INSERT INTO colors (color_name) VALUES ('red');
INSERT INTO colors (color_name) VALUES ('green');
INSERT INTO colors (color_name) VALUES ('blue');
INSERT INTO users (color_id, name, the_date) VALUES (1, 'john',
'2004-03-10');
INSERT INTO users (color_id, name, the_date) VALUES (3, 'jane',
'2004-04-12');
INSERT INTO users (color_id, name, the_date) VALUES (1, 'joe',
'2004-05-14');
INSERT INTO users (color_id, name, the_date) VALUES (2, 'jepe',
'2004-06-16');
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives? http://archives.postgresql.org 5 2532
something == otherthing is a boolean expression, you are asking the
database to compare both values, u.color_id is not equal c.color_name,
that's why you get 'f'.
I guess that you want to replace the color_id from users by the
corresponding color_name from colors:
SELECT
c.color_name, u.name, u.the_date
FROM
users u
INNER JOIN colors c ON (u.color_id=c.c olor_id)
WHERE
u.color_id = 1
ORDER BY u.the_date DESC LIMIT 1;
If you were trying to do another thing, I'm sorry, I didn't get it :(
Scott Frankel wrote: How does one embed a sub-query lookup to one table in order to replace a foreign key id number with it's name in a SELECT on a second table?
i.e.: given the following two tables, I want to replace the color_id of 1 with the color_name 'red.' (The SQL to create the two tables follows below.)
test=# SELECT * from users ; color_id | name | the_date ----------+------+------------ 1 | john | 2004-03-10 3 | jane | 2004-04-12 1 | joe | 2004-05-14 2 | jepe | 2004-06-16 (4 rows)
test=# SELECT * from colors; color_id | color_name ----------+------------ 1 | red 2 | green 3 | blue (3 rows)
My attempts yield an 'f' which looks suspiciously like a boolean false. Is there an ordering issue with my sub-query, such that the sub-query doesn't have enough info to perform its lookup?
Here's my query:
SELECT ( u.color_id = ( SELECT c.color_name FROM colors c WHERE color_id = 1)) AS color_name, u.name, u.the_date FROM users u WHERE u.color_id = 1 ORDER BY u.the_date DESC LIMIT 1;
It returns:
color_name | name | the_date ------------+------+------------ f | joe | 2004-05-14 (1 row)
Thanks! Scott
Here's the SQL to create my test tables:
CREATE TABLE colors (color_id SERIAL PRIMARY KEY, color_name text); CREATE TABLE users (color_id integer REFERENCES colors, name text, the_date date);
INSERT INTO colors (color_name) VALUES ('red'); INSERT INTO colors (color_name) VALUES ('green'); INSERT INTO colors (color_name) VALUES ('blue');
INSERT INTO users (color_id, name, the_date) VALUES (1, 'john', '2004-03-10'); INSERT INTO users (color_id, name, the_date) VALUES (3, 'jane', '2004-04-12'); INSERT INTO users (color_id, name, the_date) VALUES (1, 'joe', '2004-05-14'); INSERT INTO users (color_id, name, the_date) VALUES (2, 'jepe', '2004-06-16');
---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives?
http://archives.postgresql.org
---------------------------(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
Scott Frankel <le*****@pacbel l.net> writes: Here's my query:
SELECT ( u.color_id = ( SELECT c.color_name FROM colors c WHERE color_id = 1)) AS color_name, u.name, u.the_date FROM users u WHERE u.color_id = 1 ORDER BY u.the_date DESC LIMIT 1;
I think you want
SELECT
(SELECT c.color_name
FROM colors c
WHERE color_id = u.color_id) AS color_name,
u.name, u.the_date
FROM users u
WHERE u.color_id = 1
ORDER BY u.the_date DESC LIMIT 1;
The sub-select can refer to a variable of the outer query, as long
as you are careful to qualify it so it can't be mistaken for a variable
of the sub-select itself.
You could also express this query as a join. If you are pulling a whole
lot of users rows, the join way would probably be more efficient.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)
On Fri, Nov 12, 2004 at 09:52:09AM -0800, Scott Frankel wrote: How does one embed a sub-query lookup to one table in order to replace a foreign key id number with it's name in a SELECT on a second table?
You're talking about joins. http://www.postgresql.org/docs/7.4/s...rial-join.html http://www.postgresql.org/docs/7.4/s...pressions.html
i.e.: given the following two tables, I want to replace the color_id of 1 with the color_name 'red.' (The SQL to create the two tables follows below.)
test=# SELECT * from users ; color_id | name | the_date ----------+------+------------ 1 | john | 2004-03-10 3 | jane | 2004-04-12 1 | joe | 2004-05-14 2 | jepe | 2004-06-16
test=# SELECT * from colors; color_id | color_name ----------+------------ 1 | red 2 | green 3 | blue
There are at least four ways to write the join query you want:
SELECT color_name, name, the_date
FROM users NATURAL JOIN colors;
SELECT color_name, name, the_date
FROM users JOIN colors USING (color_id);
SELECT color_name, name, the_date
FROM users JOIN colors ON colors.color_id = users.color_id;
SELECT color_name, name, the_date
FROM users, colors
WHERE users.color_id = colors.color_id ;
Debate exists about which of the above is "better." I tend to use
the second and third forms because I think they most clearly document
how the tables are joined, and because I think of the WHERE clause
as meaning "...and here are the records I want from all that."
Of course that's just my opinion. I could be wrong.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
On Fri, Nov 12, 2004 at 11:26:14AM -0700, Michael Fuhr wrote: There are at least four ways to write the join query you want:
I may have misunderstood what results you're looking for, but the
examples I gave may nevertheless be useful. Sorry if they cause
any confusion.
--
Michael Fuhr http://www.fuhr.org/~mfuhr/
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org
Cooking with gas once again ;) Thanks for the info on JOINs!
Scott
On Nov 12, 2004, at 9:52 AM, Scott Frankel wrote: How does one embed a sub-query lookup to one table in order to replace a foreign key id number with it's name in a SELECT on a second table?
i.e.: given the following two tables, I want to replace the color_id of 1 with the color_name 'red.' (The SQL to create the two tables follows below.)
test=# SELECT * from users ; color_id | name | the_date ----------+------+------------ 1 | john | 2004-03-10 3 | jane | 2004-04-12 1 | joe | 2004-05-14 2 | jepe | 2004-06-16 (4 rows)
test=# SELECT * from colors; color_id | color_name ----------+------------ 1 | red 2 | green 3 | blue (3 rows)
My attempts yield an 'f' which looks suspiciously like a boolean false. Is there an ordering issue with my sub-query, such that the sub-query doesn't have enough info to perform its lookup?
Here's my query:
SELECT ( u.color_id = ( SELECT c.color_name FROM colors c WHERE color_id = 1)) AS color_name, u.name, u.the_date FROM users u WHERE u.color_id = 1 ORDER BY u.the_date DESC LIMIT 1;
It returns:
color_name | name | the_date ------------+------+------------ f | joe | 2004-05-14 (1 row)
Thanks! Scott
Here's the SQL to create my test tables:
CREATE TABLE colors (color_id SERIAL PRIMARY KEY, color_name text); CREATE TABLE users (color_id integer REFERENCES colors, name text, the_date date);
INSERT INTO colors (color_name) VALUES ('red'); INSERT INTO colors (color_name) VALUES ('green'); INSERT INTO colors (color_name) VALUES ('blue');
INSERT INTO users (color_id, name, the_date) VALUES (1, 'john', '2004-03-10'); INSERT INTO users (color_id, name, the_date) VALUES (3, 'jane', '2004-04-12'); INSERT INTO users (color_id, name, the_date) VALUES (1, 'joe', '2004-05-14'); INSERT INTO users (color_id, name, the_date) VALUES (2, 'jepe', '2004-06-16');
---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives?
http://archives.postgresql.org
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: tshad |
last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't
seem to get the Sub (writefile) to execute. It displays all the
response.write lines that are called directly, but not any of the
response.write lines from inside the sub.
*******************************************
<%@ Application Language="VB" %>
<script runat="server">
|
by: Colleyville Alan |
last post by:
I have a sub that can do two different tasks based on the value of one
variable. So I'd like to have two different buttons on the same form run
this, but each one setting a flag so that the conditional logic knows which
one to run.
Here is what I have in mind:
If Button_One_Was_Pressed then
link = PerfArray(iCtr).strFund_Name & ".Doc"
else
|
by: tmaster |
last post by:
When I try to dynamically add a second sub menu item to this ContextMenu
item, I get an error 'Specified argument was out of the range of valid
values'.
Private Sub mnuTopics_Show_Select(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select
Dim mShowMenuItem As MenuItem
mShowMenuItem = DirectCast(sender, MenuItem)
|
by: koorb |
last post by:
I am starting a program from a module with the Sub main procedure and
I want it to display two forms for the program's interface, but when I
run the program both forms just open and then program closes.
Dim FORM1 As New Form1
Dim FORM2 As New form2
Sub main()
FORM1.Show()
FORM2.Show()
|
by: Ron |
last post by:
Greetings,
I am trying to understand the rational for Raising Events
instead of just calling a sub. Could someone explain the
difference between the following 2 scenarios? Why would I
want to raise an event instead of just calling the sub?
Scenario1 -- Using Events
------------------------------------------------------
Public Class Form1
| |
by: dbuchanan |
last post by:
Is the following behavior normal?
Both the 'Protected sub' in the inherited form and the 'Private Shadows
sub' in the derived form fires.
My interpretation of MSDN help on the topic "Shadows" does not seem to
indicate that this is the designed behavior. (the topic is rather
cryptic to me.
Here is my code;
|
by: excelleinc.com |
last post by:
Hello,
I'm trying to put sub that's shared between all forms in my app in module so
when I make change I just change it one time.
This sub should execute and then invoke sub defaults() that's different for
each form and is contained within the forms. You'll probably ask why don't I
just put it in the form after the call to module.function, well it's a
handler for one combobox so technically I don't call it.
|
by: vitorjol |
last post by:
Hello.
I have 2 forms (Form1 and Form2). When i call the Sub placed in form1 from
form2, i get the error "object reference not set to an instance of an object"
!
What can i do to solve the problem?
Thanks
|
by: Greg Strong |
last post by:
Hello All,
Is is possible to use an ADO recordset to populate an unbound continuous
Subform?
I've done some Googling without much luck, so this maybe impossible, but
let me try to explain why. I've been exploring using Access as a front
end to both SQL MSDE and Oracle XE. I'm in the process of writing a
class to handle the basics of the ADO connection and recordsets. The
basic relationships are as follows:
|
by: YotamElal |
last post by:
Hello,
I have a popup problem.
When a popup is opened, I want all other popups to close immediatly.
(except for its self and it's child popups)
Here is my code:
code:
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
|
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,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |