Jump to content

Recommended Posts

Hi,

 

I have database of authors, and articles they've contributed.  I want to call out a list of all authors who have content that is currently "featured" on the site, or "new."

 

I don't want for example:

jim

jim

jim

karen

karen

miko

miko

miko

miko

 

Instead I'd like a list like this:

 

jim

karen

miko

 

This query below seems to freeze my site.  I must be doing something wrong here. How better to write this?

 


$sql = mysql_query ("SELECT DISTINCT registered_users.id, registered_users.user_name, video.id, video.user_id, video.title, video.pass_text FROM registered_users, video
WHERE registered_users.id = video.user_id AND video.pass_text = 'featured' OR video.pass_text = 'new' ORDER BY registered_users.user_name") or die(mysql_error());

while($row = mysql_fetch_array( $sql )) {

$author = $row[registered_users.id];

$content .= "<p>
<a href = \"page.php?id=$author\" target='_self'>$row[user_name]</a></p>";
}

 

Thanks,

Ryan

Link to comment
https://forums.phpfreaks.com/topic/225986-select-distinct-query-question/
Share on other sites

thank you -- i will definitely look into this.  but I mean the sever is unavailable for more than 15 min. it has happened twice now.

 

is the query the cause?  regardless if it's slow, is it somehow crashing the server -- and if so -- can you help me understand why?

 

thanks again, Quick

 

Ryan

I suspect it's the massive number of results that match your WHERE clause, then having to put those into a temp table to ORDER:

 

WHERE registered_users.id = video.user_id AND video.pass_text = 'featured' OR video.pass_text = 'new'

 

You should add parentheses to limit the matches:

 

probably: WHERE registered_users.id = video.user_id AND (video.pass_text = 'featured' OR video.pass_text = 'new')

 

 

Any time you have to sort/order and pull from specific fields, but only this or that but exclude this....it will consume more memory and cpu, time out even.

 

It sometimes has to run through all those results a few times just to get the desired results.

 

Indexing it properly should help it a lot, but still don't expect blazing fast results.

 

Is almost a skill to get indexing set up properly.

By using EXPLAIN SELECT you can see which index it would use or any possible keys it could use for it.

 

Here's an older article, but explains the process very nicely on how to see and what to index and the orders

http://www.databasejournal.com/features/mysql/article.php/1382791/Optimizing-MySQL-Queries-and-Indexes.htm

Thank you Quick -- I am moving in this direction for sure and reading up on it.

 

I'd like to get the query working properly -- then take some time to make sure I'm understanding optimization properly.

 

The query now works and is fast -- but DISTINCT isn't working the way I thought it would. 

 

I want to see an author's name once -- even if they have contributed 100 articles.  (Not that author's name 100 times).

 

I tried this:

(DISTINCT registered_users.id),

 

but that didn't work. 

 

I'm googling... please let me know if you have ideas on how to get each author's name echoed out once instead of several.

 

thanks again Quick!

 

 

I think the problem lies in SELECT DISTINCT, as it will only display a distinct result but based on just different values, so don't think will work like you had hoped to.

 

Essentially what you were doing is grabbing any videos and showing the users name, so it will associate the users name because the name and the video information were all truly unique when together.

 

 

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.