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

  • Home
  • SEARCH
  • 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 8117189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:04:55+00:00 2026-06-06T04:04:55+00:00

We have this statement: (SELECT res_bev.bev_id, property_value.name AS priority FROM res_bev, bev_property, property_value WHERE

  • 0

We have this statement:

(SELECT res_bev.bev_id, property_value.name AS priority 
    FROM res_bev, bev_property, property_value 
    WHERE res_bev.res_id='$resIn'
        AND bev_property.bev_id=res_bev.bev_id
        AND bev_property.type_id='23'
        AND property_value.id=bev_property.val_id)
UNION
(SELECT res_bev.bev_id, property_value.name as priority 
    FROM res_bev, bev_property, property_value 
    WHERE res_bev.res_id='$resIn' 
        AND bev_property.bev_id=res_bev.bev_id 
        AND bev_property.type_id='22' 
        AND property_value.id=bev_property.val_id)

We have Three Tables:

Res_bev
res_id | bev_id | id

Bev_property
type_id | val_id | bev_id | id

Property_value
name | id

What I am looking for is the results to be ordered by glass price(type_id=’23’) then bottle price(type_id=’22’) however it seems the union includes duplicates due to fact the first select returns say 3456 | 7.5 and the second returns 3456 | 55 since the price/Glass is 7.5 and the price/Bottle is 55; how can I eliminate these duplicates form the second SQL statement to return and ordered table?

Also, fooled with creating a pseudo-table via left joins to create a table of bev_id | price/Glass | price/Bottle, however since this should be able to expand to multiple price types I figured a UNION would be more efficient. Just a push in the right direction would be helpful.

  • 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-06-06T04:04:57+00:00Added an answer on June 6, 2026 at 4:04 am

    You can do it in 1 query by specifying bev_property.type_id to match against an IN() clause with the values inside.

    To return only the first one found you should require a DISTINCT SELECT of the accompagnying field bev_id.

    To ORDER them just add an appropriate descending ORDER BY clause. This should order first and the filter out the second bev_property.type_id value. (Databases never return anything in a specific order unless you tell them to, some might have an internal convention or it might appear they do but this is never guaranteed to be repeatable unless you specify an ORDER BY clause in your SELECT statement. )

    SELECT DISTINCT res_bev.bev_id, property_value.name AS priority 
    FROM res_bev, bev_property, property_value 
    WHERE res_bev.res_id='$resIn'
        AND bev_property.bev_id=res_bev.bev_id
        AND bev_property.type_id IN ('23','22')
        AND property_value.id=bev_property.val_id
    ORDER BY bev_property.type_id DESC;
    

    A UNION won’t really be faster since you’d have to do the whole lookup twice and if you don’t have this field indexed then you’ll do a whole table traversal with match against 1 element twice as opposed do 1 table traversal that matches against 2 elements. (walking over a whole table is what’s generally slow, not matching simple elements against each other)

    When properly indexed I think you might have a tiny overhead of executing a new select query and the query analyzer running again but I don’t know for sure. It’ll probably be smart enough to recognise the similarities between the queries so it won’t matter.

    It doesn’t always hurt to try on specific databases though. Whenever you try query optimisation with different statements use them with EXPLAIN, this will show you what the query will be doing and wether it’ll go over whole tables, sort data on file, etc…

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

Sidebar

Related Questions

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0;
I have this sql statement SELECT userID from users WHERE (name='name1' AND username='username1') OR
I have this statement: cmd = new SqlCommand(select name,lname from table1 where column1=' +
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have a site with a SELECT statement like this: SELECT * FROM MyTable
i have this statement in access: SELECT * FROM accountsnew WHERE [Panels] not like
i have this sql statement in access: SELECT * FROM (SELECT [Occurrence Number], [1
I have this simple SQL statement: select Expr1.value - Expr2.value FROM (SELECT Expr1 =
I have this working SQL statement: select oriseqs.newID from oriseqs WHERE oriseqs.singlets=1 AND oriseqs.newID
I have this sql statement: SELECT * from tBooks where BookNbr = '20111122-001' BookNbr

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.