close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

How acheive random results from a recordset that has INNER JOIN and GROUP BY in its SELECT statement?

Thread began 10/18/2017 4:58 pm by Nathon Jones Web Design | Last modified 6/13/2018 3:04 pm by Ray Borduin | 3177 views | 12 replies |

Nathon Jones Web Design

How acheive random results from a recordset that has INNER JOIN and GROUP BY in its SELECT statement?

I have three tables:
1) Categories (catID, catNAME)
2) Link Table (linkID, catID, galleryID)
3) Gallery (galleryID, domainID, galleryIMG)

I want to display a grouped list of categories on a page only where those categories exist because an image in the gallery table is linked to that category, via the link table.

The SELECT statement I've used is:

SELECT categories.catID, categories.catTITLE, link.galleryID, gallery.domainID, gallery.galleryIMG 
FROM categories
INNER JOIN link ON categories.catID = link.catID
INNER JOIN gallery ON link.galleryID = gallery.galleryID
WHERE gallery.domainID = 1 GROUP BY categories.catTITLE



This is working perfectly and I can display an image from the gallery table alongside the grouped category title.

Is it possible to make the selection of gallery.galleryIMG random though? I've tried adding RAND() but, of course, it randomises the category title, not the image.

Hope that makes sense! Thank you.
NJ

Sign in to reply to this post

Ray BorduinWebAssist

You could probably do it with a nested Select like:

php:
SELECT categories.catID, categories.catTITLE, link.galleryID, gallery.domainID, gallery.galleryIMG 

FROM categories 
INNER JOIN link ON categories.catID = link.catID 
INNER JOIN (SELECT gallery.* FROM gallery ORDER BY Rand()) AS galleryRand ON link.galleryID = galleryRand.galleryID 
WHERE gallery.domainID = 1 GROUP BY categories.catTITLE



But that isn't very efficient. It would probably make more sense to use a second recordset to get the random image after retrieving the category result.

Sign in to reply to this post
Did this help? Tips are appreciated...

Nathon Jones Web Design

Error in SQL syntax?

Sign in to reply to this post

Ray BorduinWebAssist

Sorry since I bring the table in AS another name you have to update all of the references to it like:

php:
SELECT categories.catID, categories.catTITLE, link.galleryID, galleryRand.domainID, galleryRand.galleryIMG 

FROM categories 
INNER JOIN link ON categories.catID = link.catID 
INNER JOIN (SELECT gallery.* FROM gallery ORDER BY Rand()) AS galleryRand ON link.galleryID = galleryRand.galleryID 
WHERE galleryRand.domainID = 1 GROUP BY categories.catTITLE
Sign in to reply to this post
Did this help? Tips are appreciated...

Nathon Jones Web Design

Error in SQL syntax? Have never managed to solve this.

<?php
$rsGALLERYSUBCS = new WA_MySQLi_RS("rsGALLERYSUBCS",$csdbmysqli,20);
$rsGALLERYSUBCS->setQuery("SELECT NJlinkcatsubgal.NJcatSUBID, NJcategoriesSUB.NJcatSUBTITLE, galleryRand.NJgalleryID, galleryRand.NJdomainID, galleryRand.NJgalleryIMG FROM NJlinkcatsubgal INNER JOIN NJcategoriesSUB ON NJlinkcatsubgal.NJcatSUBID = NJcategoriesSUB.NJcatSUBID INNER JOIN (SELECT NJgallery.* FROM NJgallery ORDER BY Rand()) AS galleryRand ON NJgallery.NJgalleryID = NJlinkcatsubgal.NJgalleryID WHERE galleryRand.NJdomainID = $domainID AND galleryRand.NJgalleryDELETE = 2 GROUP BY NJcategoriesSUB.NJcatSUBTITLE");
$rsGALLERYSUBCS->execute();
?>



Here's our tables and relevant fields:
NJgallery - NJgalleryID, NJgalleryIMG, NJdomainID
NJcategoriesSUB - NJcatSUBID, NJcatSUBTITLE
NJlinkcatsubgal - NJlinkcatsubgalID, NJcatSUBID, NJgalleryID (this is the link table between the gallery and the categories)


...is Rand() valid?
Hope you can clarify this one.
Appreciate any help offered.

Sign in to reply to this post

Nathon Jones Web Design

Sorry, I've updated this to:

<?php
$rsGALLERYSUBCS = new WA_MySQLi_RS("rsGALLERYSUBCS",$csdbmysqli,20);
$rsGALLERYSUBCS->setQuery("SELECT NJlinkcatsubgal.NJcatSUBID, NJcategoriesSUB.NJcatSUBTITLE, galleryRand.NJgalleryID, galleryRand.NJdomainID, galleryRand.NJgalleryIMG, galleryRand.NJgalleryTITLE FROM NJlinkcatsubgal INNER JOIN NJcategoriesSUB ON NJlinkcatsubgal.NJcatSUBID = NJcategoriesSUB.NJcatSUBID INNER JOIN (SELECT NJgallery.* FROM NJgallery ORDER BY RAND()) AS galleryRand ON NJlinkcatsubgal.NJgalleryID = galleryRand.NJgalleryID WHERE galleryRand.NJdomainID = $domainID AND galleryRand.NJgalleryDELETE = 2 GROUP BY NJcategoriesSUB.NJcatSUBTITLE");
$rsGALLERYSUBCS->execute();
?>



...which has removed the syntax error but the images aren't displaying in a random order, despite there being more than one image in each of the categories?

Thanks.
NJ

Sign in to reply to this post

Ray BorduinWebAssist

Add the ORDER BY Rand() to the end of the SQL statement instead of inside the INNER JOIN

Sign in to reply to this post
Did this help? Tips are appreciated...

Nathon Jones Web Design

Changed it to...

<?php
$rsGALLERYSUBCS = new WA_MySQLi_RS("rsGALLERYSUBCS",$csdbmysqli,20);
$rsGALLERYSUBCS->setQuery("SELECT NJlinkcatsubgal.NJcatSUBID, NJcategoriesSUB.NJcatSUBTITLE, NJgallery.NJgalleryID, NJgallery.NJdomainID, NJgallery.NJgalleryIMG, NJgallery.NJgalleryTITLE FROM NJlinkcatsubgal INNER JOIN NJcategoriesSUB ON NJlinkcatsubgal.NJcatSUBID = NJcategoriesSUB.NJcatSUBID INNER JOIN NJgallery ON NJlinkcatsubgal.NJgalleryID = NJgallery.NJgalleryID WHERE NJgallery.NJdomainID = $domainID AND NJgallery.NJgalleryDELETE = 2 GROUP BY NJcategoriesSUB.NJcatSUBTITLE ORDER BY RAND()");
$rsGALLERYSUBCS->execute();
?>



...but it's still the same images on every load.

This is available at:
www.nathonjoneswebdesign.co.uk/waterfront/photograph-gallery.php

Sign in to reply to this post

Ray BorduinWebAssist

Try running the query in PHP MyAdmin and see if it returns more than one row. Maybe the query only has one row? I see... you are grouping by NJcatSUBTITLE, so it would only have one row. That makes your original query make more sense.

Maybe it should be:

php:
<?php

$rsGALLERYSUBCS 
= new WA_MySQLi_RS("rsGALLERYSUBCS",$csdbmysqli,20);
$rsGALLERYSUBCS->setQuery("SELECT NJlinkcatsubgal.NJcatSUBID, NJcategoriesSUB.NJcatSUBTITLE, galleryRand.NJgalleryID, galleryRand.NJdomainID, galleryRand.NJgalleryIMG, galleryRand.NJgalleryTITLE FROM NJlinkcatsubgal INNER JOIN NJcategoriesSUB ON NJlinkcatsubgal.NJcatSUBID = NJcategoriesSUB.NJcatSUBID LEFT OUTER JOIN (SELECT NJgallery.* FROM NJgallery WHERE NJlinkcatsubgal.NJgalleryID = NJgallery.NJgalleryID ORDER BY RAND() LIMIT 1) AS galleryRand WHERE galleryRand.NJdomainID = $domainID AND galleryRand.NJgalleryDELETE = 2 GROUP BY NJcategoriesSUB.NJcatSUBTITLE");
$rsGALLERYSUBCS->execute();
?>
Sign in to reply to this post
Did this help? Tips are appreciated...

Nathon Jones Web Design

Error in SQL Syntax

Boy this one has me whupped!

I'm grouping by NJcatSUBTITLE because I only want one of each sub-title to be displayed. It's the accompanying image that I want randomised as there is more than one image in each sub-category.

When I run this in PHP MyAdmin:

SELECT NJlinkcatsubgal.NJcatSUBID, NJcategoriesSUB.NJcatSUBTITLE, NJgallery.NJgalleryID, NJgallery.NJdomainID, NJgallery.NJgalleryIMG, NJgallery.NJgalleryTITLE 
FROM NJlinkcatsubgal
INNER JOIN NJcategoriesSUB ON NJlinkcatsubgal.NJcatSUBID = NJcategoriesSUB.NJcatSUBID
INNER JOIN NJgallery ON NJlinkcatsubgal.NJgalleryID = NJgallery.NJgalleryID
WHERE NJgallery.NJgalleryDELETE = 2
GROUP BY NJcategoriesSUB.NJcatSUBTITLE
ORDER BY RAND()



...it returns 75 records.

It's doing everything but randomising the image.
NJ

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...