Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:41:26+00:00 2026-05-29T12:41:26+00:00

I’m trying to create a query to rank products based on onsite browsing habits

  • 0

I’m trying to create a query to rank products based on onsite browsing habits (I have provided a simplified non-dynamic query example below).

The issue I’m having is with the UNIONs in the SELECT subquery referencing other columns.

Does anyone know a clever workaround for this?

SELECT p.*, pi.piid, c.title AS cat_title, c.fn AS cat_fn, b.fn AS brand_fn, b.title AS brand_title,
(
SELECT SUM(prod_rank) AS rank FROM (
    (
        SELECT 2.95 AS prod_rank FROM prod_link_cat WHERE pid = p.pid AND link_cid = 1
    ) UNION ALL (
        SELECT 2.8 AS prod_rank FROM prod_link_cat WHERE p id = p.pid AND link_cid = 3
    ) UNION ALL (
        SELECT 0.5 AS prod_rank FROM prod_link_cat WHERE pid = p.pid AND link_cid = 2
    )
) AS tbl1
) AS rank
FROM prod p
LEFT JOIN prod_link_cat plc ON plc.pid = p.pid AND plc.position = 1
LEFT JOIN cat c ON plc.link_cid = c.cid AND c.live = 1
LEFT JOIN brand b ON b.bid = p.bid AND b.live = 1
LEFT JOIN prod_link_prod_img plpi ON plpi.pid = p.pid AND plpi.position = 1
LEFT JOIN prod_img pi ON pi.piid = plpi.link_piid AND pi.live = 1
WHERE p.live = 1
GROUP BY p.pid
ORDER BY (RAND() * rank)
LIMIT 20
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:41:27+00:00Added an answer on May 29, 2026 at 12:41 pm

    To remove your inline field query (with unions), I would rewrite it and move it as a left-join based on same criteria as your outer “prod” criteria of p.Live = 1. No sense in querying everything else where p.Live = something else.

    SELECT 
          p.*, 
          pi.piid, 
          c.title AS cat_title, 
          c.fn AS cat_fn, 
          b.fn AS brand_fn, 
          b.title AS brand_title, 
          COALESCE( PreQuery.ProdRankSum, 0 ) as ProdRankSum
       FROM 
          prod p 
             LEFT JOIN 
             ( SELECT
                     p2.id,
                     SUM( if( plc.link_cid = 1, 2.95, 0.00 )
                        + if( plc.link_cid = 2,  .50, 0.00 )
                        + if( plc.link_cid = 3, 2.80, 0.00 )) ProdRankSum
                  FROM 
                     prod p2
                        JOIN prod_link_cat plc
                           ON p2.ID = plc.pid
                          AND plc.link_cid in ( 1, 2, 3 )
                  WHERE
                     p2.Live = 1
                  GROUP BY
                     p2.id ) PreQuery
                ON p.id = PreQuery.id
    
             LEFT JOIN prod_link_cat plc 
                ON p.pid = plc.pid AND plc.position = 1 
                LEFT JOIN cat c 
                  ON plc.link_cid = c.cid AND c.live = 1 
    
             LEFT JOIN brand b 
                ON p.bid = b.bid AND b.live = 1 
    
             LEFT JOIN prod_link_prod_img plpi 
                ON p.pid = plpi.pid  AND plpi.position = 1 
                LEFT JOIN prod_img pi 
                  ON plpi.link_piid = pi.piid AND pi.live = 1
       WHERE 
          p.live = 1 
       GROUP BY 
          p.pid 
       ORDER BY 
          (RAND() * COALESCE( PreQuery.ProdRankSum, 0 )) 
       LIMIT 20 
    

    Now, after all this, you will probably have to do something about the rank, such as set the COALESCE() value other than 0… such as .001 otherwise, it will always be a zero for those products that do not have a product link cat… and ordering by a random time 0 will always be zero and thus to the top. (Or change to ORDER BY … DESC)

    HOWEVER, if you ONLY WANT those products that explicitly HAVE a link category of 1, 2 or 3, I would slightly rewrite this query too. Let me know.

    Additionally, your original query is doing left join to product link cat for the position = 1, then to cat where cat is LIVE = 1… and left joins on the others too. Did you INTEND to do left joins?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.