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
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
Kindly post your query for reference of our experts.
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
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: -
-
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
-
ORDER BY IFNULL(tc.comment_id,tc.topic_id);
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: DDonnachie |
last post by:
Hi folks,
The following SQL statment works
SELECT StaffHoursTable.EngName, StaffHoursTable.JobNumberShadow, ++++++ AS Norm_Hours, ++++++ AS OT_Hours,...
|
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...
|
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
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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: 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...
|
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...
|
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,...
| |