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 7630099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:00:45+00:00 2026-05-31T06:00:45+00:00

So I have this select statement that I am trying to make more responsive.

  • 0

So I have this select statement that I am trying to make more responsive.

I’m selecting products (items table) that got hit on a website that were listed as unavailable.

I then take the name of the product hit and see if there is any similar product in the database, and subselect the info for any similarly named products, to use as suggested alternates.

My problem is that now I want to display a url for any valid alternates, by concating a standard url with the alternate product id at the end. Unfortunately, I can’t concat said url because alt_id is a field created by a subselect and doesn’t exactly exist in the table itself, so I get the error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'alt_id' in 'field list'' in /blah/ on line 34

Here is the starting query, which works:

$data = query_array(
"select unavailable_item_hits.itemid, items.Name as unavail_name, 

(select max(items.ItemId) from items join products on items.itemid = products.itemid join brands on items.brandcode = brands.code where items.name = unavail_name and items.flagstatus != 'U' and products.flagstatus != 'U' and brands.flagstatus != 'I' and items.ItemId !=unavailable_item_hits.ItemId) as alt_id,

 unavailable_item_hits.url as url_hit, unavailable_item_hits.marketer_tag, unavailable_item_hits.count as hit_count, unavailable_item_hits.datestatus as last_date_hit from unavailable_item_hits join items on unavailable_item_hits.ItemId = items.itemid order by alt_id is not null, hit_count desc;");

Which returns this data:

itemid  unavail_name    alt_id  url_hit     marketer_tag    hit_count   last_date_hit
7615    3602 Oxford         sb.local/product.php?ItemId=7615&ProductId=55555555555555555&ref=lsf    lsf     1   2012-02-02 18:53:37
9621    McTavish    10259   sb.local/product.php?ItemId=9621&ProductId=17246&ref=lsf    lsf     2   2012-02-02 18:53:59 

Now, the part that I’d like to add is something like this:

select concat('http://example.com/products.php?ItemId=', alt_id), null) as alt_url, ...

But that doesn’t work because alt_id isn’t defined yet. I don’t know what to do from here, how to be able to use the data from that subselect in the final result. Maybe the only way is to do two different selects or something, but that will mean pulling the data out into an array in memory and then performing the subselect, which I’d like to avoid. Or maybe I have to perform the subselect again on the field that I want to manipulate like a string?

So any suggestions on clean ways to manipulate the results of a subselect?

  • 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-31T06:00:46+00:00Added an answer on May 31, 2026 at 6:00 am

    First, I wouldn’t bother the database server with things like string concatenation. Just append the alt_id to your url in the host language (PHP). Databases are good in storing and fetching large amounts of data, not calculating or string manipulation.

    If you really need to do this in the database, you can just repeat the subquery for your concat-call like

    select
        [...]
        concat('http://example.com/products.php?ItemId=', (select 
            max(items.ItemId) 
         from items 
         join products on items.itemid = products.itemid 
         join brands on items.brandcode = brands.code 
         where 
            items.name = unavail_name 
            and items.flagstatus != 'U' 
            and products.flagstatus != 'U' 
            and brands.flagstatus != 'I' 
            and items.ItemId !=unavailable_item_hits.ItemId)) as alt_url
         [...]
    

    It’s not a pretty query but that doesn’t really matter to the database server. SQL is a declarative language. You just specify what you want to have not how it should be produced. MySQL will (hopefully) see the repeated subquery and only execute it once for every tuple, or, even better, transform it into some join.

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

Sidebar

Related Questions

I'm trying to make a select statement that joins results from three in-line table
If i have a parameterized SQL statement like this: SELECT * FROM table WHERE
I have a site with a SELECT statement like this: SELECT * FROM MyTable
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have a SQL statement similar to this: SELECT COUNT(*) AS foo, SUM(foo) AS
I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0;
I am trying to solve this problem. I have a series of SELECT statements
So I recently have been trying to incorporate more sub forms to make the
I am trying to make SELECT statement for following situation and need help to

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.