Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-21-2006, 11:23 AM   #1 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Need help with a MySQL query

Here's the description of the query I need to make:

SELECT comments.*, user.user_dname (where user.user_id = comments.user_id), user.user_dname AS replyto_user_dname (where user.user_id = comments.replyto_user_id) FROM comments WHERE blog_id = bid


What I'm doing is selecting all comments made on a specific blog entry, and in each row I need to include the posters display name, and the display name of the person the poster is replying to. I was thinking I needed to perform some sort of JOIN, but then I'm not sure if I can perform conditional statements for specific fields? My brain hurts.
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-21-2006, 01:44 PM   #2 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Holy hell. I think I might have solved my problem. I figured it was a longshot, but I tried the following sql query:

Code:
$query = "	SELECT 
		comments.*, 
		users.user_dname, 
		(SELECT user_dname FROM users WHERE user_id=comments.replyto_user_id) AS replyto_user_dname 
		FROM comments, users 
		WHERE comments.blog_id='$bid' AND comments.user_id=users.user_id";
And it seems to be doing what I wanted it to do; return display names for two different user id's defined in the comments table. Any feedback as to the efficiency of this query? Is there a better, more efficient way to do it with JOINs or UNIONS or something?
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-21-2006, 01:47 PM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
SELECT c.*, u.user_dname, r.user_dname AS replyto_user_dname
FROM comments AS c, user AS u, user AS r
WHERE c.blog_id = bid, c.user_id = u.user_id, c.replyto_user_id = r.user_id

The query itself is pretty big since it needs to search in the user table twice.
DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Replication / Failover idx Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 2 02-23-2005 07:22 PM
Mysql nested query help! kitrak2004 Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 08-18-2004 06:33 AM
mysql query to populate an array dubsonic PHP 2 06-20-2003 08:07 AM
foreach() while() for() or new mysql query? nemesis PHP 6 06-10-2003 06:57 PM
and on to mysql .. sde Linux / BSD / OS X 2 01-18-2003 07:39 PM


All times are GMT -8. The time now is 08:31 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting