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
Related
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!
I have 2 tables in MS Access with the following values
Customer
id | name
1 | jon
2 | bob
3 | jack
Order
id | amount | date | customer
5 | 50 | 3/10/2017 | 1
4 | 100 | 3/10/2017 | 1
3 | 45 | 2/28/2017 | 2
2 | 10 | 3/10/2017 | 3
1 | 5 | 3/10/2017 | 2
I want to get an output of
name | orderid | amount
jon | 5 | 50
bob | 3 | 45
jack | 2 | 10
I want to get amount of the latest order id per customer, however I keep getting this
name | orderid | amount
jon | 5 | 50
jon | 4 | 100
bob | 3 | 45
bob | 2 | 10
jack | 1 | 5
I used the query designer and have used the function MAX() to the order id, GROUP BY to all columns (MS Access does not allow to group the rows using a single column), DISTINCT and DISTINCTROW, as well as set the query properties "Unique Records" to Yes but the duplicate records still shows.
In SQL I want to get total by id. Please suggest me for the query
INPUT
id | qty | price
1 | 3 | 200
1 | 4 | 225
1 | 5 | 250
2 | 7 | 300
2 | 8 | 300
3 | 3 | 500
3 | 5 | 500
3 | 6 | 500
4 | 3 | 700
4 | 2 | 745
OUTPUT
id | total
1 | 2750
2 | 4500
3 | 7000
4 | 3590
This query sums the quantity times price for all records having the same id, for each id value in your table.
SELECT id,
SUM(qty*price) AS total
FROM yourTable
GROUP BY id
I have 3 tables like the tables below
tbl_GasExpense
GID | Gas_Expense | Date_Occured
-----------------------------------
1 | 400 | 11/30/2014
2 | 500 | 11/30/2014
3 | 300 | 11/30/2014
tbl_Food Expense
FID | Food_Expense | Date_Occured
-----------------------------------
1 | 450 | 11/30/2014
2 | 250 | 11/30/2014
3 | 390 | 11/30/2014
tbl_Drink Expense
DID | Drink_Expense | Date_Occured
-----------------------------------
1 | 150 | 11/30/2014
2 | 250 | 11/30/2014
3 | 360 | 11/30/2014
and with those tables above, I want an output like this.
ID | Gas_Sum | Food_Sum | Drink_Sum | Date_Occured
-----------------------------------------------------------
1 | 1200 | 1090 | 760 | 11/30/2014
The values of the three tables from which are dated 11/30/2014 are summed in table four.
Using the IDs from the first three tables as foreign keys in the table 4 to establish a relation. Gas_Sum is a mask for GID, Food_Sum for FID, Drink_Sum for DID.
Thanks guys, but I already have my answer now after several trial and errors
.. it is something like this. but this is on my own code
SELECT o.eh_ID, SUM(o.others_amt) as 'OTHERS SUM'
FROM tbl_Others o
INNER JOIN tbl_ExpenseHead hd ON hd.eh_ID = o.eh_ID
GROUP BY o.eh_ID
I have four MySql tables (simplified here):
Table 1: factions (just a list to reference)
id | name
1 | FactionName1
2 | FactionName2
Table 2: currencies (just a list to reference)
id | name
1 | Currency1
2 | Currency2
3 | Currency3
Table 3: events (just a list to reference)
id | name | date
1 | Evebt1 | 2013-10-16
2 | Event2 | 2013-10-18 (Note: date out of order)
3 | Event3 | 2013-10-17
Table 4: event_banking (data entered after each event, remaining amount of each currency for each group)
id | faction_id | currency_id | event_id | amount
1 | 1 | 1 | 1 | 10
2 | 1 | 1 | 2 | 20
3 | 1 | 1 | 3 | 30
4 | 1 | 2 | 1 | 40
5 | 1 | 2 | 2 | 50
6 | 1 | 2 | 3 | 60
7 | 1 | 3 | 1 | 70
8 | 1 | 3 | 2 | 80
9 | 1 | 3 | 3 | 90
10 | 2 | 1 | 1 | 100
11 | 2 | 1 | 2 | 110
12 | 2 | 1 | 3 | 120
13 | 2 | 2 | 1 | 130
14 | 2 | 2 | 2 | 140
15 | 2 | 2 | 3 | 150
16 | 2 | 3 | 1 | 160
17 | 2 | 3 | 3 | 170
Note: Faction 2 didn't bank Currency 3 for Event 2
What I'm looking to be able to do is to get, for each currency, the total of the last banked (date wise) for each faction. (ie How much of each currency is currently banked in total if all factions are merged)
So, I need a table looking something like:
currency_id | total
1 | 130 (eg 20 + 110)
2 | 190 (eg 50 + 140)
3 | 250 (eg 80 + 170) <- Uses Event 3 for Group 2 as Event 2 doesn't exist
I can do basic joins etc, but I'm struggling to be able to filter the results so that I get the latest results for each Faction x Currency x Event so I can then sum them together to get the final total amounts for each currency.
I've tried various permutations of LEFT OUTER JOINs, GROUP BYss & HAVING COUNTs, and had some interesting (but incorrect results), and a variety of different error codes, but nothing remotely close to what I need.
Can anyone help?
I guess you can go on with something like this:
select eb.currency_id, sum(amount) as total
from events e
inner join (
select faction_id, currency_id, max(date) as md
from event_banking eb
inner join events e
on eb.event_id = e.id
group by faction_id, currency_id
) a
on e.date = a.md
inner join event_banking eb
on e.id = eb.event_id
and a.faction_id = eb.faction_id
and a.currency_id = eb.currency_id
group by currency_id;
Here is SQL Fiddle