472,958 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

SQL join query help

I have written a forum and am using the following query to search in it:

$query="select topics.tid,f_messages.messid from f_messages left join
topics on f_messages.tid = topics.tid where ($title_list) OR ($msg_list)";

I trimmed it a bit to save space here (the last two are just strings).

It works fine, but it's returning more than once the same topics.tid

I want it to return only unique topic id's.

distinct doesn't work. I think it has to do with the f_messages.tid =
topics.tid as it isn't using distinct there.

I tried returning distinct(f_messages.tid) instead but again, no help!

This is using mysql. Re-structuring the db isn't an option.

Thanks for any light on this one, I can only think of dumping the tid's
into a TEMP dbase and selecting distinct from there, although it sure is
messy!
--
po**@sixbit.org
SDF Public Access UNIX System - http://sdf.lonestar.org
Jul 16 '05 #1
2 3837
In article <be************@ID-30799.news.uni-berlin.de>, Agelmar wrote:
This should produce an output of something like
Topic ID | Message ID
1 |1
1 |2
1 |3
2 |7
2 |13
3 |4
3 |5
...
Right, thing is, on a forum search you don't want to see the same topic a
gazillion times.

I want unique topic id's, which goes against my query.

The (group by topics.tid) part seemed to reduce this, but I'm not getting
unique (distinct) tid's.
unless of course you only care if *some* message in a given thread matches,
and you just want the first message in the thread that matches and don't
care about the rest...
Yeah.
in this case, you can just do something like:

$query="select topics.tid, MIN(f_messages.messid) from f_messages left join
topics on f_messages.tid = topics.tid GROUP BY topics.tid where
($title_list) OR ($msg_list)";

I hope this helps, I must admit I am not totally sure that I understand your
question, but I gave it my best and I hope this is what you're looking for.


Thanks, this actually solved it, where above I have duplicate tid's even
after your addition this is due to proper db duplications which shouldn't
be there.

Thanks!

On a side note I had to put the GROUP BY at the end of the query to get it
to work.

--
po**@sixbit.org
SDF Public Access UNIX System - http://sdf.lonestar.org
Jul 16 '05 #2
poff <po**@sixbit.org> wrote in message
news:<sl*****************@otaku.freeshell.org>...

I have written a forum and am using the following query to search in it:

$query="select topics.tid,f_messages.messid from f_messages left join
topics on f_messages.tid = topics.tid where ($title_list) OR ($msg_list)";

I trimmed it a bit to save space here (the last two are just strings).

It works fine, but it's returning more than once the same topics.tid
And it should... Let's say topic number 5 has three messages in it
(messid = 123, 135, and 148), so if you run

SELECT topics.tid, f_messages.messid
FROM f_messages LEFT JOIN topics
ON f_messages.tid = topics.tid
WHERE topics.tid = 5;

you will get something like this back:

+------------+-------------------+
| topics.tid | f_messages.messid |
+------------+-------------------+
| 5 | 123 |
| 5 | 135 |
| 5 | 148 |
+------------+-------------------+

Remember, DISTINCT applies to an ENTIRE ROW IN THE RESULT SET,
NOT to a single field and NOT to an entire record in the source
table. So the three records in the result set above are NOT
distinct and will all be returned.
I want it to return only unique topic id's.


Then ask for them accordingly:

SELECT DISTINCT tid FROM topics WHERE [your WHERE clause];

Also, it seems to me you are trying to fetch the first (and only
the first) message from each topic; this can be done like this:

SELECT topics.tid AS topic, MIN(f_messages.messid) as message
FROM f_messages LEFT JOIN topics
ON f_messages.tid = topics.tid
WHERE [your WHERE clause]
GROUP BY topics.tid;

Cheers,
NC
Jul 16 '05 #3

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

Similar topics

0
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...
2
by: Good Man | last post by:
Hi there Yes, I've read about JOINs, albeit after coding for a couple of years already using queries like the following: "SELECT m.LastName, m.FirstName, o.Address FROM members m, offices o...
7
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...
1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
2
by: dskillingstad | last post by:
I would really appreciate someone's help on this, or at least point me in the right direction.... I'm working on a permit database that contains 12 tables, and rather than list all of the...
4
by: bhargav.desai | last post by:
Hello Gurus, I need help! I have two table, tblCurrent and tblPrevious. What I want to join the tables, and create a new table that have matching records from both the tables, plus this new...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
21
by: CSN | last post by:
I have a pretty simple select query that joins a table (p) with 125K rows with another table (pc) with almost one million rows: select p.* from product_categories pc inner join products p on...
52
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...
3
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.