Jump to content

Little MySQL help


MrXHellboy

Recommended Posts

Little MySQL help needed... Nobody is answering on the MySQL part on the forums

 

I have this query

(
		SELECT os_comments.comment_author AS author, os_posts.post_title AS title,os_posts.guid AS guid, os_comments.comment_date AS date
		FROM os_comments, os_posts
		WHERE os_comments.comment_post_ID = os_posts.ID
		AND os_posts.post_status = 'publish'
		AND os_comments.comment_approved = 1
		ORDER BY os_comments.comment_date
		DESC
		LIMIT 0,10
		)UNION(
		SELECT dna_comments.comment_author AS author, dna_posts.post_title AS title,dna_posts.guid AS guid, dna_comments.comment_date AS date
		FROM dna_comments, dna_posts
		WHERE dna_comments.comment_post_ID = dna_posts.ID
		AND dna_posts.post_status = 'publish'
		AND dna_comments.comment_approved = 1
		ORDER BY dna_comments.comment_date
		DESC
		LIMIT 0,10
		)

 

But now i want 10 total instead of 10 of each.....

 

Can someone help me out herE ?

Link to comment
Share on other sites

I've tried already this

                         "(
		SELECT os_comments.comment_author AS author, os_posts.post_title AS title,os_posts.guid AS guid, os_comments.comment_date AS date
		FROM os_comments, os_posts
		WHERE os_comments.comment_post_ID = os_posts.ID
		AND os_posts.post_status = 'publish'
		AND os_comments.comment_approved = 1
		ORDER BY os_comments.comment_date
		DESC

		)UNION(
		SELECT dna_comments.comment_author AS author, dna_posts.post_title AS title,dna_posts.guid AS guid, dna_comments.comment_date AS date
		FROM dna_comments, dna_posts
		WHERE dna_comments.comment_post_ID = dna_posts.ID
		AND dna_posts.post_status = 'publish'
		AND dna_comments.comment_approved = 1
		ORDER BY dna_comments.comment_date
		DESC

		)
                        LIMIT 10
		"

 

But somehow, now, it will only take the last 10 of os_ prefix. Another idea or am i doing something wrong ?

Link to comment
Share on other sites

No, i want a total of 10. sorted on date. But i did read something about a bug with UNION+LIMIT. I've tested it, and it seems that the limit outside the parentheses only affects the first SELECT query.

 

All i want is the LAST 10 (total) comments from both tables. I dont need 10 of each, just a total of 10, sorted on date.

 

Link to comment
Share on other sites

Sure -- the idea is that you want no more than 10 from each table, and then when you have a max of 20 records, re-sort and limit again.

 

(
SELECT os_comments.comment_author AS author, os_posts.post_title AS title,os_posts.guid AS guid, os_comments.comment_date AS date
FROM os_comments, os_posts
WHERE os_comments.comment_post_ID = os_posts.ID
AND os_posts.post_status = 'publish'
AND os_comments.comment_approved = 1
ORDER BY os_comments.comment_date DESC LIMIT 10
)
UNION ALL
(
SELECT dna_comments.comment_author AS author, dna_posts.post_title AS title,dna_posts.guid AS guid, dna_comments.comment_date AS date
FROM dna_comments, dna_posts
WHERE dna_comments.comment_post_ID = dna_posts.ID
AND dna_posts.post_status = 'publish'
AND dna_comments.comment_approved = 1
ORDER BY dna_comments.comment_date DESC LIMIT 10
)
ORDER BY comment_date DESC LIMIT 10

Link to comment
Share on other sites

Hi Fenway,

 

Unfortunately this is not working. Its returns a empty result set.

This is the code i have right now.

 

The if construct in the foreach just takes care that the limit is 10, because i have 20 in total. I want the if within the foreach gone and just get 10 results total. I use the ksort because somehow ORDER BY ist really working with me :(

		$comments = $wpdb->get_results("(
		SELECT os_comments.comment_author AS author, os_posts.post_title AS title,os_posts.guid AS guid, os_comments.comment_date AS date
		FROM os_comments, os_posts
		WHERE os_comments.comment_post_ID = os_posts.ID
		AND os_posts.post_status = 'publish'
		AND os_comments.comment_approved = 1
		ORDER BY os_comments.comment_date
		DESC
		LIMIT 10
		)UNION ALL(
		SELECT dna_comments.comment_author AS author, dna_posts.post_title AS title,dna_posts.guid AS guid, dna_comments.comment_date AS date
		FROM dna_comments, dna_posts
		WHERE dna_comments.comment_post_ID = dna_posts.ID
		AND dna_posts.post_status = 'publish'
		AND dna_comments.comment_approved = 1
		ORDER BY dna_comments.comment_date
		DESC
		LIMIT 10
		)
		", ARRAY_A);

		foreach ($comments as $c)
		{
			$arr[$c['date']] = $c;
		}

	#print_r($arr);
	krsort($arr);
	$Count = 0;
		$CommentsList = '<ul>';
		foreach ($arr as $comment):
			if ($Count == $instance['amount'])
				break;
			$CommentsList .= '<li>'. $comment['author'] .' left a comment on <a href="'. htmlspecialchars($comment['guid']) .'">'. $comment['title'].'</a></li>';
			$Count++;
		endforeach;
		$CommentsList .= '</ul>';

	return $CommentsList;

 

Please read this if you want. http://www.mail-archive.com/sqlite-users@sqlite.org/msg55669.html

 

Link to comment
Share on other sites

I meant the first piece of code. . . . My bad...

 

However, i checked again the mysql manual, but according to them, you would be able to use the LIMIT outside the parentheses. I've tried those, i even tried to perform it on a very small table with just 2 fields but still the same result.

Link to comment
Share on other sites

But how to do it ? I just want 10 records total. No 20, they are useless records, the time wont make the difference here with just 10 records, but i don't want wasted records.

You have no choice -- you need 10 of each to get 10 combined & sorted correctly.  Don't worry about it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.