[web] Making 2 SQL Queries into 1

Started by
2 comments, last by coderx75 15 years, 10 months ago
Hello, I was recently doing some coding for some integration with my frontpage of my website, and the forum system (YAF)...and I ran into a common issue where I need pieces of info from different tables.

Info Needed

Table: yaf_topic
Fields Needed: Topic(varchar),TopicID(int)

Table: yaf_message
Fields Needed: Message(varchar), Posted(dateTime)
What I always do in these situation is pretty much just use 2 queries

Query1
"SELECT Topic, TopicID FROM yaf_topic WHERE ForumID = 4"

then Query2
"SELECT Message, Posted FROM yaf_message WHERE TopicID = (topicID from above)"
I just figured it's time to move to the next level and combine the 2...I just don't know how... Sorry for the newb question, anyone who takes the time to answer has my thanks. -ArchG
Advertisement
SELECT Topic, TopicID, Message, Posted FROM yaf_topic INNER JOIN yaf_message.TopicID ON yaf_topic.TopicID = yaf_message WHERE ForumID = 4

or

SELECT Topic, TopicID, Message, Posted FROM yaf_topic NATURAL JOIN yaf_message WHERE ForumID = 4
much appreciated Colin Jeanne
This should do it...

SELECT yt.TopicID, yt.Topic, ym.Message, ym.Posted
FROM yaf_topic yt
INNER JOIN yaf_message ym ON ym.TopicID = yt.TopicID
WHERE ForumID = @ForumID
Quit screwin' around! - Brock Samson

This topic is closed to new replies.

Advertisement