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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:50:52+00:00 2026-05-23T06:50:52+00:00

I have three simple tables as shown below: +—-+————-+ +—-+———–+ +—-+—————+ | artists |

  • 0

I have three simple tables as shown below:

+----+-------------+  +----+-----------+  +----+---------------+
| artists          |  | albums         |  | songs              |
+----+-------------+  +----+-----------+  +----+---------------+
| id | name        |  | id | name      |  | id | name          |
+----+-------------+  +----+-----------+  +----+---------------+
| 0  | The Beatles |  | 0  | Help!     |  | 0  | Help!         |
| 1  | Metallica   |  | 1  | Metallica |  | 1  | Enter Sandman |
+----+-------------+  +----+-----------+  +----+---------------+

And I would like a query to result in the following:

+--------+---------------+
| type   | name          |
+--------+---------------+
| artist | The Beatles   |
| artist | Metallica     |
| album  | Help!         |
| album  | Metallica     |
| song   | Help!         |
| song   | Enter Sandman |
+--------+---------------+

I am trying to use a LIKE query to search Artists, Albums, and Songs.

Right now I use the SQL below to grab the results from the three columns, then I use Ruby to bring them together in one object. I figure it would be much more efficient to use pure SQL rather than the interpreted language to return the results.

SELECT name FROM artists WHERE name LIKE 'something%'
SELECT name FROM albums WHERE name LIKE 'something%'
SELECT name FROM songs WHERE name LIKE 'something%'

I am not so great with SQL and have been struggling with this for a few days and was wondering if anyone on here could point me in the right direction.

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

    You could, as others have suggested, use UNION. The semantics of UNION, however, force the database to eliminate duplicate rows which generally requires a sort operation. Since you know that each query necessarily returns non-duplicate rows, it would be more efficient to use UNION ALL. Something like

    SELECT *
      FROM (SELECT 'artists' as type, name 
              FROM artists 
            UNION ALL
            SELECT 'albums' as type, name 
              FROM albums
            UNION ALL
            SELECT 'songs' as type, name 
              FROM songs)
     WHERE name LIKE 'something%'
    

    Whatever database engine you are using should be able to push the predicate on NAME into each of the three branches that are being UNION ALL’ed together. But it would be worth confirming that in the query plan.

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

Sidebar

Related Questions

Hi I'm using ms2005 for a simple calendaring system. We have three 'legacy' tables:
I have a page with three simple html forms which submit to various tables
This should be straightforward. I have three tables (relevant columns shown). users : ID,USERNAME,PWD
I have a simple three table reference. It looks plain enough. The associations there
I basically have three tables, posts, images and postimages (this simply contains the ids
I have three simple models - Car , Details and Details2 . Car have
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a simple object that allows you to assign three properties (x,y,z) (lets
I have a very simple Setup project that copies three dlls into the GAC.

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.