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

Complex query question

For some reason I can't figure out the following. I am using mysql and php.

I have the following 2 tables

tl_topic //a topic
topic_id //id of the topic
title //the title of the topic
comment //the body of the topic
user_id //the user who posted the topic

tl_topic_comment //comments on a topic
topic_id
comment_id

both topic_id and comment_id are exact date/times that it was added.

I want to get a list of topics ordered by latest comment date (comment_id), and if there are no comments on a particular topic I need to use the topic_id to order it

Any help would be appreciated
Thanks
Feb 20 '08 #1
4 1044
I want to get a list of topics ordered by latest comment date (comment_id), and if there are no comments on a particular topic I need to use the topic_id to order it
Let me just be clear what I am asking for, I am just looking for the mySQL SELECT statement which would give me all the topics in the correct order.

I was trying to do it with temperary tables and joins but I think there might be an easier way

The first topic would be the newest one added or the one with the latest comment

Thanks Again
Feb 20 '08 #2
debasisdas
8,127 Expert 4TB
Kindly post your query for reference of our experts.
Feb 20 '08 #3
well right now I have:

[PHP]$sql = "SELECT tl_topic.topic_id, tl_topic.title, tl_topic.user_id FROM tl_team_topic, tl_topic WHERE tl_team_topic.team_id = '$team_id' AND tl_team_topic.topic_id = tl_topic.topic_id ORDER BY tl_topic.topic_id DESC"[/PHP]

and this gets all the topics in the right order if none had comments. but I want to sort by topic_id and the latest comment on each topic. If there are no comments we just use topic_id but if there comments we need to sort by comment_id.

see the posts above to see the tables i'm working with

thanks
Feb 20 '08 #4
amitpatel66
2,367 Expert 2GB
For some reason I can't figure out the following. I am using mysql and php.

I have the following 2 tables

tl_topic //a topic
topic_id //id of the topic
title //the title of the topic
comment //the body of the topic
user_id //the user who posted the topic

tl_topic_comment //comments on a topic
topic_id
comment_id

both topic_id and comment_id are exact date/times that it was added.

I want to get a list of topics ordered by latest comment date (comment_id), and if there are no comments on a particular topic I need to use the topic_id to order it

Any help would be appreciated
Thanks
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT tt.user_id,tt.title,tt.comment,tc.topic_id,tc.comment_id FROM tl_topic tt,tl_topic_comment tc WHERE tt.topic_id = tc.topic_id
  3. ORDER BY IFNULL(tc.comment_id,tc.topic_id);
  4.  
  5.  
Feb 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
2
by: Mikel | last post by:
I am trying to get around the problem "The expression you have entered is too complex" for a select query. (The example below is not the expression that is giving me headaches.) So I am thinking...
1
by: Giloosh | last post by:
hello, i dont know if what i want to do is really considered complex, but i sure cannot figure it out. i need to create a query that shows the payments a person makes every day for a variable...
8
by: Matt | last post by:
Hi all, Thank you for taking the time. I have a database with 45 tables on it. 44 tables are linked to a main table through a one to one relationship. My question is, is there no way i can...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
6
by: pippapippa | last post by:
I should be most grateful for a little advice. I have used Access 2000 & latterly 2002. Am about to upgrade since it is evident that documentation, tutorials etc are more readily available in...
13
by: DDonnachie | last post by:
Hi folks, The following SQL statment works SELECT StaffHoursTable.EngName, StaffHoursTable.JobNumberShadow, ++++++ AS Norm_Hours, ++++++ AS OT_Hours,...
3
by: BUmed | last post by:
Ok let me start from the start. I have a form that has question in it and the person chooses 0 1 2 -99 for each. The form then needs to add up the numbers for the sub categories in the form. For...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.