473,773 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IN or JOIN

please CC me as I am on digest
-------------------------------
I have three tables, simplified for, well, simplicity :-)

CREATE TABLE Usrs (
usr_id serial primary NOT NULL,
name text NOT NULL,
login text NOT NULL,
CONSTRAINT PK_Usrs PRIMARY KEY (usr_id)
);

CREATE TABLE EmailAddrs (
email_addr_id SERIAL NOT NULL,
email_addr VARCHAR(255) NOT NULL UNIQUE,
CONSTRAINT PK_EmailAddrs PRIMARY KEY (email_addr_id)
);

CREATE TABLE UsrEmails (
usr_id INT4 NOT NULL,
email_addr_id INT4 NOT NULL,
CONSTRAINT PK_UsrEmails PRIMARY KEY (usr_id, email_addr_id)
);

ALTER TABLE UsrEmails
ADD CONSTRAINT EmailAddrs11_0M UsrEmail
FOREIGN KEY (email_addr_id)
REFERENCES EmailAddrs (email_addr_id) ;

ALTER TABLE UsrEmails
ADD CONSTRAINT UsrEmailTypes11 _0MUsrEmails
FOREIGN KEY (usr_email_type _id)
REFERENCES UsrEmailTypes (usr_email_type _id);
multiple 'Usrs' can have the same name, but different logins.
I want to find the count of usrs that:
have the name 'some_name'
and
have the email 'some_email'

-----------------------------
Should I use a JOIN or an IN?
If the 'IN' example below is right, and there is either:

NO Usr with name='some_name '
OR
NO email with email='some_ema il'

will it return a NULL, or a '0' count?

my thought for an IN:
----------------------
SELECT COUNT(*)
FROM UsrEmails
WHERE
usr_id IN
(SELECT usr_id
FROM Usrs
WHERE name='some_name '::text)
AND
email_addr_id=( SELECT email_addr_id
FROM Emails
WHERE email='some_ema il'::text);



---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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

Nov 23 '05 #1
2 1147
I can't imagine a scenario where the IN would be a better choice. Is
there a reason you think the JOIN is not appropriate?

-tfo

On Sep 25, 2004, at 7:20 PM, ge*****@fireser ve.net wrote:
please CC me as I am on digest
-------------------------------
I have three tables, simplified for, well, simplicity :-)

CREATE TABLE Usrs (
usr_id serial primary NOT NULL,
name text NOT NULL,
login text NOT NULL,
CONSTRAINT PK_Usrs PRIMARY KEY (usr_id)
);

CREATE TABLE EmailAddrs (
email_addr_id SERIAL NOT NULL,
email_addr VARCHAR(255) NOT NULL UNIQUE,
CONSTRAINT PK_EmailAddrs PRIMARY KEY (email_addr_id)
);

CREATE TABLE UsrEmails (
usr_id INT4 NOT NULL,
email_addr_id INT4 NOT NULL,
CONSTRAINT PK_UsrEmails PRIMARY KEY (usr_id, email_addr_id)
);

ALTER TABLE UsrEmails
ADD CONSTRAINT EmailAddrs11_0M UsrEmail
FOREIGN KEY (email_addr_id)
REFERENCES EmailAddrs (email_addr_id) ;

ALTER TABLE UsrEmails
ADD CONSTRAINT UsrEmailTypes11 _0MUsrEmails
FOREIGN KEY (usr_email_type _id)
REFERENCES UsrEmailTypes (usr_email_type _id);
multiple 'Usrs' can have the same name, but different logins.
I want to find the count of usrs that:
have the name 'some_name'
and
have the email 'some_email'

-----------------------------
Should I use a JOIN or an IN?
If the 'IN' example below is right, and there is either:

NO Usr with name='some_name '
OR
NO email with email='some_ema il'

will it return a NULL, or a '0' count?

my thought for an IN:
----------------------
SELECT COUNT(*)
FROM UsrEmails
WHERE
usr_id IN
(SELECT usr_id
FROM Usrs
WHERE name='some_name '::text)
AND
email_addr_id=( SELECT email_addr_id
FROM Emails
WHERE email='some_ema il'::text);



---------------------------(end of
broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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

Nov 23 '05 #2
No, I am just moving beyond table, foreign key, function, trigger, and contraint designing to more complicated queries. I just didn't know enough to choose if either would WORK at all, much less be better.
Thomas F. O'Connell wrote:
I can't imagine a scenario where the IN would be a better choice. Is
there a reason you think the JOIN is not appropriate?

-tfo

On Sep 25, 2004, at 7:20 PM, ge*****@fireser ve.net wrote:
please CC me as I am on digest
-------------------------------
I have three tables, simplified for, well, simplicity :-)

CREATE TABLE Usrs (
usr_id serial primary NOT NULL,
name text NOT NULL,
login text NOT NULL,
CONSTRAINT PK_Usrs PRIMARY KEY (usr_id)
);

CREATE TABLE EmailAddrs (
email_addr_id SERIAL NOT NULL,
email_addr VARCHAR(255) NOT NULL UNIQUE,
CONSTRAINT PK_EmailAddrs PRIMARY KEY (email_addr_id)
);

CREATE TABLE UsrEmails (
usr_id INT4 NOT NULL,
email_addr_id INT4 NOT NULL,
CONSTRAINT PK_UsrEmails PRIMARY KEY (usr_id, email_addr_id)
);

ALTER TABLE UsrEmails
ADD CONSTRAINT EmailAddrs11_0M UsrEmail
FOREIGN KEY (email_addr_id)
REFERENCES EmailAddrs (email_addr_id) ;

ALTER TABLE UsrEmails
ADD CONSTRAINT UsrEmailTypes11 _0MUsrEmails
FOREIGN KEY (usr_email_type _id)
REFERENCES UsrEmailTypes (usr_email_type _id);
multiple 'Usrs' can have the same name, but different logins.
I want to find the count of usrs that:
have the name 'some_name'
and
have the email 'some_email'

-----------------------------
Should I use a JOIN or an IN?
If the 'IN' example below is right, and there is either:

NO Usr with name='some_name '
OR
NO email with email='some_ema il'

will it return a NULL, or a '0' count?

my thought for an IN:
----------------------
SELECT COUNT(*)
FROM UsrEmails
WHERE
usr_id IN
(SELECT usr_id
FROM Usrs
WHERE name='some_name '::text)
AND
email_addr_id=( SELECT email_addr_id
FROM Emails
WHERE email='some_ema il'::text);



---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3

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

Similar topics

0
3072
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
1949
by: Bruce Duncan | last post by:
I'm a bit new to MySQL (know MS SQL well...and that may be the problem...getting the syntax confused) and I'm having a join problem...can anyone offer some help? Here's my problem: I have table1 that needs to "left" join to table1A, table1B, and table1C which is corrently done with the following: select table1.x, table1a.y, table1b.z, table1c.q from table1 left join table1a on table1.ID = table1a.ID left join table1b on table1.ID =...
3
3355
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four inner joins, as follows : SELECT DISTINCT upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori gin,associatekey.username,associatekey2.username,upcards.deleted FROM upcards,status,origins,associates INNER JOIN status...
1
2270
by: Beachvolleyballer | last post by:
hi there anyone had an idea to join following 2 queries to 1???? ----- QUERY 1 --------------------------------------------- SELECT TMS_CaseF_2.Name AS TCDomain_0, TMS_CaseF_3.Name AS TCDomain_1, TMS.CaseF.Name AS TCFolder_2, TMS_CaseF_1.Name AS TCFolder_3,
8
4975
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and also display their corresponding entries in arb, but if there is NO entry in arb I still want it to show up as NULL or something, so that I can get the attention that there IS no language associated with that article.
7
1685
by: Greg | last post by:
I'm a quantitative securities analyst working with Compustat data (company fiscal reports and pricing feeds). My coworker came across a problem that we fixed, but I'd like to understand 'why' it was happening and just don't get it yet. Here's the starting query (reduced to simple prefixes): ----INITIAL-----
3
23100
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but he doesn't know how to do that, or even if he can, and i don't have to time to learn DB2 from scratch right now. The following SQL Query is a trimmed sample of the full View (i.e. Logical) definition - and i would create it on an SQL based...
12
18676
by: Phil Powell | last post by:
<cfquery name="getAll" datasource="#request.dsn#"> SELECT U.userID, U.fname, U.lname, U.phone, U.lastLoggedIn, U.choiceId, U.experience, T.label AS teamLabel, R.label AS roleLabel FROM User U LEFT JOIN UserTeamAssoc UTA ON UTA.userID = U.userID, Role R, UserRoleAssoc URA, Team T WHERE U.userID = URA.userID AND URA.roleID = R.roleID AND U.userId > 1
52
6351
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
12
13191
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
0
9621
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
9454
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
10264
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
8937
agi2029
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...
1
7463
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
5355
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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.