473,408 Members | 2,839 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,408 software developers and data experts.

single query for multiple table lookup based on third table

Hello,

Exim 4 integrated with MySQL 4.

I have four tables:

CREATE TABLE users (
uid bigint(10) unsigned DEFAULT '' NOT NULL auto_increment,
listemail varchar(200) DEFAULT '' NOT NULL ,
PRIMARY KEY (uid),
);

CREATE TABLE groups (
gid int(11) DEFAULT '' NOT NULL auto_increment,
listemail longtext ,
UNIQUE gid (gid),
);

CREATE TABLE mailalias (
mid int(11) DEFAULT '' NOT NULL auto_increment,
listemail longtext ,
UNIQUE mid (mid)
);

CREATE TABLE mailgroups (
id int(10) DEFAULT '' NOT NULL auto_increment,
uid int(11) ,
gid int(11) ,
mid int(11) DEFAULT '0' NOT NULL ,
PRIMARY KEY (id)
);

mailgroups contains relations between mailalias, users and groups:
select * from mailgroups;
+----+------+------+-----+
| id | uid | gid | mid |
+----+------+------+-----+
| 1 | NULL | 3 | 17 |
| 2 | NULL | 32 | 17 |
| 3 | 61 | NULL | 17 |
| 4 | 72 | NULL | 17 |
+----+------+------+-----+
What I need to do is:

Given mailalias.listemail, get users.listemail and groups.listemail
that are part of mailalias.listemail. Ie: if mid is 17, find gid 3 and
32, uid 61 and 72. I found in the manual a way to do it with two
queries:

SELECT users.listemail FROM users,mailgroups,mailalias WHERE
mailgroups.uid = users.uid AND mailgroups.mid = mailalias.mid AND
mailalias.listemail
='$LISTEMAIL'

and then the same query with groups instead of users.

But because this is run within Exim, I need this in one query.
Basically, given an email address, I need to find the users and groups
that are associated with that address.

Any help would be appreciated.
Thanks
Kimo R.
Jul 20 '05 #1
3 3024
Kimo R. wrote:
SELECT users.listemail FROM users,mailgroups,mailalias WHERE
mailgroups.uid = users.uid AND mailgroups.mid = mailalias.mid AND
mailalias.listemail
='$LISTEMAIL'

and then the same query with groups instead of users.

But because this is run within Exim, I need this in one query.
Basically, given an email address, I need to find the users and groups
that are associated with that address.


I tried this on my test database and it seems to do what you describe:

select coalesce(u.listemail, g.listemail)
from mailalias a inner join mailgroups m on (a.mid = m.mid)
left outer join users u on (m.uid = u.uid)
left outer join groups g on (m.gid = g.gid)
where a.listemail = '$LISTEMAIL'

Regards,
Bill K.
Jul 20 '05 #2
Bill Karwin <bi**@karwin.com> wrote in message news:<cf*********@enews1.newsguy.com>...
Kimo R. wrote:

But because this is run within Exim, I need this in one query.
Basically, given an email address, I need to find the users and groups
that are associated with that address.
select coalesce(u.listemail, g.listemail)
from mailalias a inner join mailgroups m on (a.mid = m.mid)
left outer join users u on (m.uid = u.uid)
left outer join groups g on (m.gid = g.gid)
where a.listemail = '$LISTEMAIL'


Perfect. Thank you very much.
Kimo R.
Jul 20 '05 #3
Just combine these two queries with UNION ALL:

SELECT users.listemail FROM users,mailgroups,mailalias
WHERE
mailgroups.uid = users.uid
AND mailgroups.mid = mailalias.mid
AND mailalias.listemail='$LISTEMAIL'
UNION ALL
SELECT groups.listemail FROM groups,mailgroups,mailalias
WHERE
mailgroups.gid = groups.gid
AND mailgroups.mid = mailalias.mid
AND mailalias.listemail='$LISTEMAIL'
"Kimo R." <ki*****@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
SELECT users.listemail FROM users,mailgroups,mailalias WHERE
mailgroups.uid = users.uid AND mailgroups.mid = mailalias.mid AND
mailalias.listemail
='$LISTEMAIL'

and then the same query with groups instead of users.

But because this is run within Exim, I need this in one query.
Basically, given an email address, I need to find the users and groups
that are associated with that address.


Jul 20 '05 #4

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

Similar topics

3
by: Kimo R. | last post by:
Hello, Exim 4 integrated with MySQL 4. I have four tables: CREATE TABLE users ( uid bigint(10) unsigned DEFAULT '' NOT NULL auto_increment, listemail varchar(200) DEFAULT '' NOT NULL ,...
10
by: jqq | last post by:
SQL2K on W2Kserver I need some help revamping a rather complicated query. I've given the table and existing query information below. (FYI, changing the database structure is right out.) The...
3
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
1
by: Robert | last post by:
I am trying to create a db for service providers by county. I'm relatively new to db programming, but I have done quite a bit of programming ranging from the old basic days up to doing some...
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...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
4
by: dancole42 | last post by:
So I have an invoicing database based on two main forms: Orders and OrderLines. Orders has fields like: OrderID BillingMethod OrderDate CreditCard CCExp OrdSubTotal ShippingCharge
3
by: epiphyte | last post by:
Let's say I have a table with a list of young folk (table1). This table is related to a table that lists all their booty calls (table2). A third table lists their booty calls' booty calls (table3)....
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
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: 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
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...
0
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...
0
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,...
0
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...

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.