473,587 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replacing subselect with joins

Hi.

I have a query that looks like this:

SELECT user_id, user_given_name s, user_surname, user_cast FROM users WHERE
user_id NOT IN (SELECT user_id FROM linkage WHERE group_id=1)
ORDER BY users.user_surn ame

What this does is return the users who are not in group 1. linkage
contains user_id, group_id pairs of users who are in what groups. What I
would like to do is write this not using subselects, as they are not
supported in older mysql versions.

So, is it possible to write a query (1 query) using mysql 3.x or 4.0 SQL
that will return the users that don't exist in a specific group when you
have the following table structure (simplified for this example):

users:
user_id

linkage:
user_id, group_id

Users can exists in several groups, that is why I use the linkage table,
otherwise it could be done in an easier way. I tried to experiment using
some joins but I'm stuck. Is subselects the only way? Writing 2 queries is
possible, of course, but I'd rather have it all in one.

Thanks in advance.

--
Mattias Nordstrom
Jul 20 '05 #1
5 2455
Mattias Nordstrom wrote:
SELECT user_id, user_given_name s, user_surname, user_cast FROM users WHERE
user_id NOT IN (SELECT user_id FROM linkage WHERE group_id=1)
ORDER BY users.user_surn ame


select user_id, user_given_name s, user_surname, user_cast
from users
left join linkage on linkage.user_id =users.user_id
and linkage.group_i d=1
where linkage.user_id is null;
Jul 20 '05 #2
On Fri, 18 Jun 2004 21:02:03 +0000, Aggro wrote:
Mattias Nordstrom wrote:
SELECT user_id, user_given_name s, user_surname, user_cast FROM users WHERE
user_id NOT IN (SELECT user_id FROM linkage WHERE group_id=1)
ORDER BY users.user_surn ame


select user_id, user_given_name s, user_surname, user_cast
from users
left join linkage on linkage.user_id =users.user_id
and linkage.group_i d=1
where linkage.user_id is null;


Thanks! That works.

--
Mattias Nordstrom
Jul 20 '05 #3
On Fri, 18 Jun 2004 21:02:03 +0000, Aggro wrote:
Mattias Nordstrom wrote:
SELECT user_id, user_given_name s, user_surname, user_cast FROM users WHERE
user_id NOT IN (SELECT user_id FROM linkage WHERE group_id=1)
ORDER BY users.user_surn ame


select user_id, user_given_name s, user_surname, user_cast
from users
left join linkage on linkage.user_id =users.user_id
and linkage.group_i d=1
where linkage.user_id is null;


No, apparently that returns all users. I'd like it to return only the ones
that are not in group_id=1. i.e. should not return the ones that are in
group_id=1, which it does now.

Any ideas?

--
Mattias Nordstrom

Jul 20 '05 #4
Mattias Nordstrom wrote:
No, apparently that returns all users. I'd like it to return only the ones
that are not in group_id=1. i.e. should not return the ones that are in
group_id=1, which it does now.


There was one error in my query, which does give syntax error atleast on
MySQL version 3.x, but after fixing that, it should work correctly.
Let me show you (I only put user_id and user_surname to users table,
other columns should follow them automaticly, so adding columns won't
make a difference.):

-----start from mysql console----------------------------------
mysql> create table users(user_id int, user_surname varchar(255) );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into users values(1,'a'),( 2,'b'),(3,'c'),
-> (4,'d'),(5,'e') ,(6,'f'),(7,'g' );
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> create table linkage(user_id int, group_id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into linkage values(1,1),(1, 2),(1,3),(2,2),
-> (2,3),(2,4),(3, 1),(4,2),(6,1), (7,2),(7,4);
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0

mysql> select * from users;
+---------+--------------+
| user_id | user_surname |
+---------+--------------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | f |
| 7 | g |
+---------+--------------+
7 rows in set (0.00 sec)

mysql> select * from linkage;
+---------+----------+
| user_id | group_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 1 |
| 4 | 2 |
| 6 | 1 |
| 7 | 2 |
| 7 | 4 |
+---------+----------+
11 rows in set (0.00 sec)

mysql> select users.user_id, users.user_surn ame
-> from users
-> left join linkage on linkage.user_id =users.user_id
-> and linkage.group_i d=1
-> where linkage.user_id is null;
+---------+--------------+
| user_id | user_surname |
+---------+--------------+
| 2 | b |
| 4 | d |
| 5 | e |
| 7 | g |
+---------+--------------+
4 rows in set (0.00 sec)
-----end from mysql console----------------------------------

We had 7 users, but only 4 of them are returned, none of the users which
are in group 1 are returned. Am I misunderstandin g something or have I
missed anything?

Can you provide a similar test case that shows how this doesn't work?
Jul 20 '05 #5


SELECT u.user_id, u.user_given_na mes, u.user_surname, u.user_cast
FROM users u, (SELECT user.user_id
FROM user, linkage
WHERE linkage.user_id =user.user_id
GROUP BY user.user_id
HAVING count(*)=SUM(CA SE WHEN group_id = 1 THEN 0 ELSE 1 END)
) uid
WHERE
u.user_id = uid.user_id
ORDER BY u.user_surname

"Mattias Nordstrom" <ma***@use.ne t> wrote in message
news:pa******** *************** *****@use.net.. .
Hi.

I have a query that looks like this:

SELECT user_id, user_given_name s, user_surname, user_cast FROM users WHERE
user_id NOT IN (SELECT user_id FROM linkage WHERE group_id=1)
ORDER BY users.user_surn ame

What this does is return the users who are not in group 1. linkage
contains user_id, group_id pairs of users who are in what groups. What I
would like to do is write this not using subselects, as they are not
supported in older mysql versions.

So, is it possible to write a query (1 query) using mysql 3.x or 4.0 SQL
that will return the users that don't exist in a specific group when you
have the following table structure (simplified for this example):

users:
user_id

linkage:
user_id, group_id

Users can exists in several groups, that is why I use the linkage table,
otherwise it could be done in an easier way. I tried to experiment using
some joins but I'm stuck. Is subselects the only way? Writing 2 queries is
possible, of course, but I'd rather have it all in one.

Thanks in advance.

--
Mattias Nordstrom


Jul 20 '05 #6

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

Similar topics

11
2298
by: Steven Stern | last post by:
I'm stuck on a host that is still running MYSQL version 3. I need to flatten out a relationship to AND a set of criteria. In a nutshell User UserID UserEmail UserOther
0
1738
by: limbert | last post by:
------=_NextPart_000_0001_01C34D7B.DFBF53C0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, =20 I was reading the manual and it said that the subselect is only
1
1356
by: Magnus | last post by:
Hi, I have previously worked mostly with Sql Server and wonder how to write a subselect query in mysql 4.0. What i would like to do is something like: select a.name, b.isbn from author a left outer join isbn b on b.authorid in(select id from author)
7
4553
by: Alex | last post by:
Hi, I have some problems with creating a query that will replace values in one table from another one. Table 1: userName : refCode1 : refCode2 ------------------------------ alex : 12 : 24
3
7183
by: Neil Zanella | last post by:
Hello, I would like to ask the about the following... PostgreSQL allows tables resulting from subselects to be renamed with an optional AS keyword whereas Oracle 9 will report an error whenever a table is renamed with the AS keyword. Furthermore, in PostgreSQL when the result of a subselect is referenced in an outer select it is required...
4
2736
by: James | last post by:
I have a performance problem with the following query and variations on the subselect. The EXISTS version of the first example will complete in ~10 minutes. The NOT logic in both the examples makes them both keep running long enough that a communications error is the only result returned so far. This is a federated view computer ~160 k...
2
7347
by: Morten K. Poulsen | last post by:
(re-post) Dear list, Please let me know if this is not the list to ask this kind of question. I am trying to optimize a query that joins two relatively large (750000 rows in each) tables. If I do it using a subselect, I can "force" the planner to choose the fastest path. Now, my question is:
6
5005
by: Sebastien | last post by:
I have the following statement which I run successfully in... 1 hour 10 minutes. SELECT a.tsgicd as ACCT_ID, a.tsa5cd as SEC_ID, CASE WHEN (SUBSTRING(a.tsgicd, 6, 1) = 'R' or SUBSTRING(a.tsgicd, 6, 1) = 'Y' or SUBSTRING(a.tsgicd, 6, 1) = '0'
0
1915
by: 23db | last post by:
I have the following three tables: OFFICE, EMPLOYEE, SYSTEM. OFFICE joins to EMPLOYEE on OFFICE_ID (OFFICE can have zero to many EMPLOYEE's), SYSTEM joins to EMPLOYEE on EMPLOYEE_ID and OFFICE_ID since EMPLOYEE_ID is not unique. For each EMPLOYEE record there can be zero or more rows in SYSTEM. The SYSTEM.TYPE_ID is numeric and has values...
0
7920
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...
0
8215
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. ...
1
7973
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...
0
8220
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...
0
6626
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...
0
5394
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...
0
3844
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...
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.