473,387 Members | 1,789 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,387 software developers and data experts.

Odd query from multiple tables (brainbuster)

I'm wondering if something like this can be done in mysql...

I have two tables I want to pull data from. One table will contain a
unique user_id which I could pull out like:

select user_id from table;

that's basically all I would need from that table. The second table
contains a user id field, but can have several duplicate user_id's in
the table. Essentially I would need:

select distinct user_id from table2 where user_id != owner_id;

Then between the results of the two queries, I would like to remove any
duplicate user_id's. I can do this easy enough in PHP, but I would
prefer to increase my mysql skills. Is it possible to somehow combine
these two queries into one and pull out the uniques using distinct?

Thanks in advance!

Apr 26 '06 #1
3 1667
xhenxhe wrote:
I'm wondering if something like this can be done in mysql...

I have two tables I want to pull data from. One table will contain a
unique user_id which I could pull out like:

select user_id from table;

that's basically all I would need from that table. The second table
contains a user id field, but can have several duplicate user_id's in
the table. Essentially I would need:

select distinct user_id from table2 where user_id != owner_id;

Then between the results of the two queries, I would like to remove any
duplicate user_id's. I can do this easy enough in PHP, but I would
prefer to increase my mysql skills. Is it possible to somehow combine
these two queries into one and pull out the uniques using distinct?


Sure, but is it just a list of distinct user_id's what you really want?
Or do you want the full row corresponding to one of the occurrances of
a given user_id in table2?

In other words, you can do this:

SELECT DISTINCT table1.user_id
FROM table1 JOIN table2 USING (user_id)
WHERE table2.user_id != table2.owner_id

Instead of using DISTINCT, you can also filter for those entries that
only occur once:

SELECT table1.user_id
FROM table1 JOIN table2 USING (user_id)
WHERE table2.user_id != table2.owner_id
GROUP BY table1.user_id
HAVING COUNT(*) = 1

(or HAVING COUNT(*) > 1 if you want to return the user_id's for which
there are duplicates)

What if there are user_id values in table2 that don't occur in table1,
or vice versa? You'd need to use an outer join to make sure you
retrieve these.

If you want to get other attributes from table2 besides just the
user_id, the query gets more complex.

Regards,
Bill K.
Apr 26 '06 #2
Thanks Bill. I guess I should be more specific so the answer can be
more helpful to me :) So I'll be very specific...

I have a freelance site at http://www.nerdlance.com. Each project has a
"project clarification board" (PCB). Whenever there is a new post on
the PCB, I would like to send an email to everyone that has 1) posted a
bid on the project and 2) Anyone that has posted to the PCB. So it is
possible that someone can post to the PCB withought placing a bid.
Obviously, I don't want the message to be sent to the same person
twice, so I can't have duplicates. Also, I wouldn't want the email to
go to the person that just posted to the PCB, or the project owner, as
his/her message will be slightly different.

The data I'm retrieving will come from two tables. Table 1 is "bids"
and table 2 is "projectclarify". Among other fields, "bids" has the
following: bid_id, user_id, & project_id. "projectclarify" has the
these fields: clarify_id, project_id, from_id (from_id is the user_id
that posted the project).

So, is it possible to join these tables like you mentioned if the
user_id field has a different name in the second table?

An example to look at would be http://www.nerdlance.com/p67, which has
a few bids and a couple of PCB posts, though no examples of a PCB post
without a bid.

Like I mentioned, I can figure out ways to do this with two queries,
then manage the data with PHP, but I'd like to know if its possible to
get all the info I need with just one query.

Thanks!
-Dustin

Apr 28 '06 #3
Oh, I think I figured it out. I just join my two queries with UNION :)

select b.user_id
from bids b
where b.project_id=95 -- current project
union
select pc.from_id as user_id
from projectclarify pc
where pc.project_id=95 -- current project
and pc.from_id != 269 -- project owner id
and pc.from_id != 2 -- poster_id

Apr 28 '06 #4

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

Similar topics

8
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
4
by: | last post by:
I have an ACCESS db from a DEXA machine (bone scanner). The data is in seperate tables and I want to link them so that I can query and create one table with all the data I need from the seperate...
2
by: Scott Cannon | last post by:
I am trying to query 3 tables all related by Clinet_ID. The Clients table, Monthly_Expenses table and Monthly_Income table. Each client can have 0>M instances of expenses, past due expenses, and...
2
by: Josh Felker | last post by:
Hey everyone. I have a daily log form with my investing info. In that form I have 2 subforms, the first shows my profits on all the stocks I traded that day, the second shows my profits on...
0
by: Ellen Ricca | last post by:
I have an Access db with several ODBC linked ORACLE tables. These tables have multiple-field PK's. The tables work just fine in many diff types of queries including unmatched queries that are...
1
by: hmiller | last post by:
I'm sorry to populate the server with yet another question about linking multiple tables and queries, howerver I have not been able to find the right criteria. My problem. I am trying to...
5
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
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...
1
by: crazdandconfusd | last post by:
I have a database with two tables I use for shipping information. One is for if I'm only shipping one item and the other is for a back page of a report for when I ship multiple items. If I'm...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.