Sub Select Question for MySql

I am trying to get this to work.  I need to only return matches fields only containing ProductImagesLarge/

I tried to create it, but I don't know if this is the right strategy and not sure what to do with the where clause.

select * from (select products.Catalog2_URL, products.Catalog3_URL, products.Catalog4_URL, products.Catalog5_URL, products.Catalog6_URL
FROM products) x WHERE Instr(x,'ProductImagesLarge/') > 0


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
x is the query alias, not a field's alias.
you want to check this for each field individually...

so, this would be what you are looking for:
select *
 from (
  select Catalog2_URL URL from products
  select Catalog3_URL from products
  select Catalog4_URL from products
   select Catalog5_URL from products
   select Catalog6_URL from products
  )  x 
WHERE Instr(URL,'ProductImagesLarge/') > 0

Open in new window

however, a more (db-) efficient query would be:
select Catalog2_URL URL from products where  Instr(Catalog2_URL ,'ProductImagesLarge/') > 0
select Catalog3_URL URL from products where  Instr(Catalog3_URL ,'ProductImagesLarge/') > 0
etc etc

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nemws1Database AdministratorCommented:
Okay, well, this isn't quite what you want.  You want to search for "ProductImagesLarge/" in multiple different fields in a table.  The sub-select you have here won't do what you want - it'll still return a multi-field table - not concat all the fields into a single-field (virtual) table.  You can do this, albeit a bit complexly, with UNION (assuming all the _URL fields are defined the same):

(SELECT Catalog2_URL AS URL from products WHERE INSTR(Catalog2_URL, 'ProductImagesLarge/') > 0)
(SELECT Catalog3_URL AS URL from products WHERE INSTR(Catalog3_URL, 'ProductImagesLarge/') > 0)
(SELECT Catalog4_URL AS URL from products WHERE INSTR(Catalog4_URL, 'ProductImagesLarge/') > 0)
(SELECT Catalog5_URL AS URL from products WHERE INSTR(Catalog5_URL, 'ProductImagesLarge/') > 0)
(SELECT Catalog6_URL AS URL from products WHERE INSTR(Catalog6_URL, 'ProductImagesLarge/') > 0)

Open in new window

nemws1Database AdministratorCommented:
Agh!  angelIII beat me to it. ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.