Solved

mySQL query, good data structure to handle an exception

Posted on 2015-01-21
12
66 Views
Last Modified: 2015-04-22
Hello SQL specialists,

I have a table with some prices, as follows:

City    Price  
-----   -----  
NY      17      
SF      16      
Moscow  12      
Lima     1      
Berlin  21      
-------------

When I want to find the price, I can handle the query, with the city name as input. No problem.
But now I need to deal with an exception: in only one city, the prices depend on some additional field:

City    Price   Buyers Age
-----   -----   ----------
NY      17      nil
SF      16      nil
Moscow  12      nil
Lima     1      nil
Berlin   5      Child
Berlin  21     MiddleAge
Berlin   7      Retired
--------------------------

Is there any way to make a query, with input Buyers Age and input City, and output price ?
Example: The buyers age is Child, and he is in Lima: what is the price ?

Should I somehow make a 2nd table, to solve it ?

Best regards, Sonja
0
Comment
Question by:Sonja_M
  • 6
  • 6
12 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40562199
SELECT Price FROM table WHERE BuyersAge = 'Child' AND City='Lima'

HTH,
Dan
0
 

Author Comment

by:Sonja_M
ID: 40562255
Hi Dan,

can you explain me your solution ?: is NIL matched to just anything ?

- Sonja
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40562283
What's NIL got to do with anything?

The query will simply scan the table for any value 'Child' in the BuyersAge column, then take those results and do another scan in column City for 'Lima', then return the results.

If you plan to run that query often, I would add indexes on those columns.

Tip: you can always use EXPLAIN before a SELECT so MySQL will give you the execution plan for those queries.
EXPLAIN SELECT Price FROM table WHERE BuyersAge = 'Child' AND City='Lima'
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Sonja_M
ID: 40562327
Dan,
thank you for your quick answer, however, according to your description of the scan, I would expect no matching record. So once again my question: does a not set value ("nil") just match to whatever value you have in the query ?
-Sonja
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40562355
Nope. Using your sample data, the query will return no results, as there is no 'Child' in 'Lima'.

After the first condition, the result set will look like this:
City    Price   Buyers Age
-----   -----   ----------
Berlin   5      Child
--------------------------

Then MySQL will search for 'Lima' in the City column, will not find it and return no result.
0
 

Author Comment

by:Sonja_M
ID: 40562381
okay. This means that, after you indeed read my question, you see that your answers did not solve my question.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40562461
OK. Then you'll have to rephrase the question.

This is what I read:
Example: The buyers age is Child, and he is in Lima: what is the price ?

The query in my first post answers that question. There is no child in Lima, so you'll get 0 rows back.
0
 

Author Comment

by:Sonja_M
ID: 40562477
no.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40562501
:))
Next mind reader, please :)
0
 

Accepted Solution

by:
Sonja_M earned 0 total points
ID: 40562523
Dan, I have no time for those who answer before reading the question. Goodbye.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40562553
This is usually called "the curse of knowledge".

You spent some time pondering this question, so it's obvious to you. The key word here is "you".

The rest of us have to figure the problem based on what you wrote.
For the next person that will read this thread, please try to write an explicit question.

Goodbye.
0
 

Author Closing Comment

by:Sonja_M
ID: 40737565
blabla
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Error 3 43
Multilanguage Database Design in MySQL 5 117
How to count in a table in php 22 36
Excel - SQL export question 3 42
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question