select all rows containing two values based off of another column - sql-server-2008

Let's say I have a table that looks like the below. It's basically a table containing history of sales. There's other columns, but I'm only putting the ones that matter here for what I am trying to figure out with how to do SQL query on.
What I want to do is get an output in where I can see all sales in where only papaya and apple were bought.
Sales history Table
--------------------------------------
salesnumber | salesdesc
1 | papaya
1 | apple
2 | apple
3 | watermelon
4 | papaya
4 | apple
5 | pomegranate
6 | watermelon
7 | apple
8 | watermelon
9 | orange
10 | orange
Desired Output
--------------------------------------
salesnumber | salesdesc
1 | papaya
1 | apple
4 | papaya
4 | apple

You could try:
Select * from sales_history_table where salesdesc = 'papaya' or salesdesc = 'apple'

Related

SQL query select from multiple tables problem [duplicate]

This question already has answers here:
Many-to-many relationships examples
(5 answers)
Closed 1 year ago.
I have a mySQL database containing where Schools located in different countries provide training (the product) in different cars, with different objectives.
SCHOOLS
id| schoolname |
----------------------------------
1 | Pete’s Driving school |
2 | Karen’s Driving school |
3 | John’s Driving school |
4 | Donald’s Driving school |
CARS
id| carbrand |
----------------------------------
1 | Buick |
2 | Mercedes |
3 | Tesla |
4 | Audi |
PROVIDES (PROVIDES THIS TRAINING)
id| name |
----------------------------------
1 | Get License |
2 | Practise driving |
3 | Maneuvering |
4 | Winter training |
LOCATIONS
id| name |
----------------------------------
1 | USA |
2 | UK |
3 | France |
4 | Japan |
I also have these tables telling me which schools have which cars, which location, and provide which training:
SCHOOL_OWNS
id| schoolid | carsid
----------------------------------
1 | 1 | 2
2 | 1 | 1
3 | 2 | 1
4 | 3 | 2
SCHOOL_PROVIDES
id| schoolid | providesid
----------------------------------
1 | 1 | 2
2 | 1 | 3
3 | 2 | 1
4 | 3 | 2
SCHOOL_LOCATIONS
id| schoolid | locationsid
----------------------------------
1 | 1 | 2
2 | 2 | 4
3 | 2 | 1
4 | 3 | 2
My products which I'm trying to select and output..
PRODUCT
id| productname | schoolid | carid | locationid | price
----------------------------------
1 | Product1 | 2 | 1 | 4 | 400 USD
2 | Product2 | 3 | 1 | 2 | 300 USD
3 | Product3 | 1 | 2 | 1 | 200 USD
4 | Product4 | 2 | 2 | 1 | 100 USD
My question is now how to write a JOIN query to get what I'm looking for. This is doing my head in and not even sure where to start or if the way I have done it is a recommended way of setting it up?
I’m trying to create a SQL query that lets me select and sort depending on three criteria, car brand, provides and location.
I have on the top of the page three dropdown menus "CARS", "PROVIDES SERVICE" and "LOCATION".
What SQL Query could match these two scenarios?! Thanks I'm a bit lost..
1. If I want to display all products that fulfill all
these three criteria (Buick, Maneuvering, Japan)
cars=1&provides=3&location=4
Output (only one product meet this criteria):
Product1 | Karen’s Driving school | 400 USD
-------------------
2. And if I only want display all products that provide ‘practise driving’ ?cars=&provides=2&location= ,
Then all products should show that have a company that provides “practice driving” ..
Output (only one two products meet this criteria):
Product1 | Karen’s Driving school | 400 USD
Product3 | John’s Driving school | 200 USD
it looks like you can't make those joins since you do not have foreign keys in your tables.
as an example.. linking cars id to a provides id will not work, so it can't reach that table an easy way... you will need a foreign key or make a sub table to make it easier for yourself!

How to create multiple related tables in mysql database

My title might not really convey what i'm trying to achieve, I have some tables which are related but i can't figure out how to create this relationship to make storing and retrieving data much easy. Below is what i was able to come up with.
color table
colorId(PK)
productId(FK)
colorName
size table
sizeId(PK)
productId(FK)
size
product table
productId(PK)
priceId(FK)
Qty
Name
Title
price table
priceId(PK)
productId(FK)
Now my problem is i have product with different variety for example
Men's blue addidas glide running shoes with productId 1, this product have different sizes and different color and the prices varies on sizes and color. example, if this pair of shoe color BLUE size 11 sells for $50, the same pair of shoe with bigger size and different color say size 12 color red might sell for $55, also this same pair of shoe color blue might have only size 11 available in stock and color blue size 12 of this pair might not be available, how do i create my table to save all the differences between color and sizes, and also prices. Any help on this, thanks
You probably shouldnt have every single attribute in it's own table, a potentially better way would be to have a product table and a variant table. This way you could have price, size, color, quantity available, etc all in the same table, then join on the product i.e. Men's blue adidas glide running shoes.
Your variant table would look something like this:
| variant_id | product_id | price | colour_id | size_id | quantity_available | in_stock |
| 1 | 1 | 50.00 | 1 | 11 | 20 | 1 |
| 2 | 1 | 55.00 | 2 | 12 | 0 | 1 |
Then you could load each variant individually, and have the quantity_available updated whenever you make a sale. I've also included an in_stock boolean so you can override whether stock appears on your site without having to adjust the quantity_available.
You'd then have the variant_id on the purchase table so you can join these later. Also, I like having sizes and colours on their own tables so you can put a taxonomy in place to aggregate all they blues say, or all the XS, S, M, L, XL.
Something like:
| colour_id | name | base_colour_id |
| 1 | Royal Blue | 3 |
| 2 | Spanish Blue | 3 |
| 3 | Blue | 3 |
| 4 | Ultramarine | 3 |
| 5 | Green | 5 |
| 6 | Mint Green | 5 |
This way you can add more unique colour variants and still report on base colours or full colour names. Here you'd use: SELECT * FROM variant_colour a JOIN variant_colour b ON b.colour_id = a.base_colour_id you'd get:
| colour_id | name | base_colour_id | colour_id | name | base_colour_id |
| 1 | Royal Blue | 3 | 3 | Blue | 3 |
| 2 | Spanish Blue | 3 | 3 | Blue | 3 |
| 3 | Blue | 3 | 3 | Blue | 3 |
| 4 | Ultramarine | 3 | 3 | Blue | 3 |
| 5 | Green | 5 | 5 | Green | 5 |
| 6 | Mint Green | 5 | 5 | Green | 5 |
This same idea can be used for sizes etc so you don't end up with hundreds of unique attribute variants that make your info impossible to report on.

Selecting rows except some others

ID | fruit | who | days_ago |
----------------------------------
1 | orange | Adam | 2 |
2 | banana | Adam | 3 |
3 | apple | Adam | 4 |
4 | kiwi | Adam | 2 |
6 | banana | Jimmy | 3 |
7 | apple | Jimmy | 5 |
8 | grapes | Jimmy | 1 |
9 | orange | Carol | 2 |
10 | grapes | Carol | 6 |
11 | lemon | Carol | 3 |
And my problem is:
The table contained information about who bought what fruit and when (when is an extra information I just need to keep).
I need to select all the fruits that Adam didn't buy.
ID | fruit | who | days_ago |
----------------------------------
8 | grapes | Jimmy | 1 |
10 | grapes | Carol | 6 |
11 | lemon | Carol | 3 |
And if Jimmy bought them, I don't want to know if Carol bought them too.
And my result should be this:
ID | fruit | who | days_ago |
----------------------------------
8 | grapes | Jimmy | 1 |
11 | lemon | Carol | 3 |
When I GROUP BY fruit, I lose information about who and days_ago (I don't understand how they're chosen).
And when I select unique fruits and drop all that Adam bought, I lose grapes which both Jimmy and Carol bought.
This isn't the actual table I'm working on. Just a simplification of what I'm struggling with.
Any ideas?
If you want the fruits that Adam didn't buy, you can use a subquery for this. Have the subquery select the fruits he did buy, and then have the main query say "not those".
SELECT *
FROM fruits
WHERE fruit NOT IN(
SELECT fruit
FROM fruits
WHERE who = "Adam"
)
DEMO: http://sqlfiddle.com/#!9/3974d/1
When you use GROUP BY, you are combining multiple rows into one. The other rows' data isn't gone, just hidden. Try using GROUP_CONCAT to see the list of who fields.
SELECT ID, fruit,
GROUP_CONCAT(who) as who,
GROUP_CONCAT(days_ago) AS days_ago
FROM fruits
WHERE fruit NOT IN(
SELECT fruit
FROM fruits
WHERE who = "Adam"
)
GROUP BY fruit
DEMO: http://sqlfiddle.com/#!9/3974d/3

mysql query for conditions on related tables

i have a table 'house' with a house id, a table 'room' with a room id, and a relation table for those two tables
HOUSE
-----
1 | house1
2 | house2
3 | house3
4 | house4
5 | house5
ROOM
------
1 | kitchen
2 | bathroom
3 | garage
HOUSE_ROOMS
------------
id | house_id | room_id | size
=================================
1 | 1 | 1 | 200
2 | 1 | 2 | 300
3 | 2 | 1 | 400
4 | 2 | 2 | 500
5 | 3 | 1 | 500
6 | 4 | 2 | 600
7 | 5 | 1 | 400
8 | 5 | 5 | 300
I'm having trouble writing a query that returns an array of house id's by some combined conditions:
example: get all houses with a kitchen sized between 350 and 450 AND a bathroom sized between 450 and 550
-> result should be an array containing house2
anyone know how i should write this query?
Assuming all your IDs are fixed, the following quick query will work:
SELECT HOUSE_ID
FROM HOUSE_ROOMS
WHERE (ROOM_ID=2 AND SIZE>=450 AND SIZE<=550)
OR (ROOM_ID=1 AND SIZE>=350 AND SIZE<=450)
GROUP BY HOUSE_ID
HAVING COUNT(DISTINCT ROOM_ID)>1

database structure for multiple options on products

I have products in my database (MySQL).
I have my prices based on different combinations of options for my products.
I have the following data:
PRODUCTS
prodID | prodName
1 | Ball
OPTIONS
optionID | optionName | prodID
1 | color | 1
2 | size | 1
3 | material | 1
OPTIONVALUES
ovID | optionID | ovValue
1 | 1 | red
2 | 1 | blue
3 | 2 | small
4 | 2 | big
5 | 3 | wood
6 | 3 | glass
for prices I would like to have like this:
red small wood ball - $13
red small glass ball - 14
red big wood ball - 16
red big glass ball - 17
blue small wood ball - 12.5
[...]
How can I design the database structure for this?
I have tried several ways but none was good. if the number of the options were static then every option could have its own table, but this was that is not a good solution.
I like what you already have. If you want it to be fully flexible as i guess you do, i would do the following:
Add two more Tables.
Product_Instance
instanceID | prodID | price
1 | 1 | 13
Product_Instance_Options
instanceID | ovID
1 | 1
1 | 3
1 | 6
In this way you could define all possible combinations and prices for them. Might be a hell of a lot work to set up all the prices, but if you cant calculate them (like glass = +2$, small=2$ / big=4$) you will have this however you do it.
How about something like this:
productTypes
prodID | prodName
1 | Ball
2 | Car
productInstances
prodInstanceID | prodID | Prize
1 | 1 | $19
2 | 1 | $50
3 | 2 | $8
properties
propertyID | propertyName
1 | color
2 | size
3 | material
propertiesValues
pvID |propertyID | propertyName
1 | 1 | red
2 | 1 | blue
3 | 2 | small
4 | 2 | medium
5 | 2 | large
6 | 3 | wood
7 | 3 | glass
productProperties
ppID | prodInstanceID | pvID
1 | 1 | 1
2 | 1 | 3
3 | 1 | 6
4 | 2 | 2
5 | 2 | 4
6 | 2 | 7
7 | 3 | 1
8 | 3 | 3
9 | 3 | 6
In your design you can't determine which option belongs to which product instance. If you look at your table optionvalues, there is no way to group together your options.