I have a table hotel [hotelid,hotelname,etc]
and another table facilities[facilityid,facilityname]
these 2 tables are linked through table hotel_to_facilities_map[hotelid,facility_id]
so the table hotel_to_facilities_map might contain values as
hotelid facility_id
-------------------
1 3
1 5
1 6
2 6
2 2
2 5
now i want to retrieve all the hotels which match ALL facilities asked for
select * from hotel_to_facilities_map where facility_id IN (3,5,2)
but this will cause the match as an OR Expression while i need AND.
is there any workaround or solution for this?
select hotelid
from hotel_to_facilities_map
where facility_id in (3,5,2)
group by hotelid
having count(*) = 3
Related
Initially we have such a table
contact_id
group_id
1
1
2
1
2
3
3
1
3
3
3
2
1
2
After that I make a query to search for groups containing the values of contacts 1 and 3
SELECT `group_id` ,COUNT(DISTINCT(`contact_id`)) AS `variants`
FROM `TaskTeam_member`
WHERE `contact_id`='1' OR `contact_id`='3'
GROUP BY `group_id`
HAVING `variants`='2'
it turns out that such a table (correct)
contact_id
variants
1
2
2
2
And now I need to add in addition to searching for values 1 and 3 in the group to check the total number of elements in it (I need 2), that is, if presumably there are elements 1 and 3 in group 1, but the total number of elements is 3 and not 2 as in the example above, then this group should not be output
the result should be like this
contact_id
variants
2
2
help me complete my request!
If you want to check for other contact_ids you need to include all records, but only count the ones you want:
SELECT `group_id`
FROM `TaskTeam_member`
GROUP BY `group_id`
HAVING COUNT(DISTINCT contact_id)=2 AND COUNT(DISTINCT CASE contact_id WHEN 1 THEN 1 WHEN 3 THEN 3 END)=2
I'm trying to create an sql (mariadb) request that select multiples columns but need two columns to be a unique pair but making sure the pair selected has its created_at value the least than the other duplicata pairs.
Here is what my table approximately looks like :
id
from_user_id
to_user_id
created_at
1
1
2
1000000005
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
5
6
5
100000006
I made this table precise to explain the request I want.
So I want to select the distinct pair (from_user_id, to_user_id) implying that the couple (1,2) which could also be (2,1) should be unique. The second rule is it should pick the couple with the minimum created_at value.
So the result table I want is :
id
from_user_id
to_user_id
created_at
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
2,1,1000000002 because the created_at is lesser than the other same couple case (1,2,1000000005).
In this case if I want only the values above created_at:999999999 to be selected I just have to add one condition.
I really hope my question is clear. I'm struggling to make distinct pairs work with other columns.
Thanks in advance for your answers.
WITH
cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY GREATEST(from_user_id,to_user_id),
LEAST(from_user_id,to_user_id)
ORDER BY created_at) rn
FROM table
)
SELECT *
FROM cte
WHERE rn = 1
I'm defining the relationship between the two tables using a join table. I want to arrange them in the order of many overlapping things. Currently, we are using subquery, is there a way to get the same result using join?
People FoodTable PeopleFood
ID | NAME ID | Food ID | PeopleId | FoodId
1 BOB 1 Hamberger 1 1 1
2 JOHN 2 Pizza 2 1 2
3 KATY 3 Chicken 3 1 3
4 MILLER 4 Salad 4 2 1
5 AMANDA 5 Sushi 5 2 2
6 2 3
7 3 2
8 3 3
9 4 3
10 4 5
11 5 5
When the table is defined in this way, I want to arrange food tastes similar to Bob's.
I'm doing it like this now.
SELECT people_id, COUNT(people_id) as count
FROM peopleFood
WHERE food_id IN
(SELECT food_id FROM peopleFood
WHERE people_id = 1)
AND people_id != 1
GROUP BY people_id
ORDER BY count DESC;
-- Result -------------
People_id | count
2 3
3 2
4 1
Is there a better way to change this method or use join?
Thank you!!!
You have been inconsistent in your use of the table and column names -
Tables - PeopleFood in your sample data but you reference peopleFood in your query.
Columns - PeopleId and FoodId in your sample data but you reference people_id and food_id in your query.
Choose a naming convention and stick to it. Everyone has there own preference but the important thing is to be consistent.
The equivalent query with INNER JOIN instead of your sub-query is -
SELECT
`pf2`.`people_id`,
COUNT(`pf2`.`food_id`) as `count`
FROM `PeopleFood` `pf1`
INNER JOIN `PeopleFood` `pf2`
ON `pf2`.`people_id` <> `pf1`.`people_id`
AND `pf2`.`food_id` = `pf1`.`food_id`
WHERE `pf1`.`people_id` = 1
GROUP BY `pf2`.`people_id`
ORDER BY `count` DESC;
The performance difference between the two queries is unlikely to be noticeable and it might be argued that the intent is clearer in your version with the sub-query.
The surrogate key ID on your PeopleFood table should be dropped in favour of the compound “natural” primary key on people_id and food_id.
The Cost of Useless Surrogate Keys in Relationship Tables
Inner join:
SELECT p.People_id, COUNT(p.People_id) as count FROM PeopleTable p
INNER JOIN FoodTable f
ON(p.People_id = f.FoodId)
WHERE people = 1
GROUP BY p.people_id
ORDER BY count DESC;
If it helps, please mark it as an accepted answer!
I have two tables that I would like to join in MYSQL and I'm looking for the most optimized way to do this. Here's the problem:
I want to count the number of records based on a field (call it customer) in each table then join the results together (using customer) to produce a summary table. Note that all customers must be returned even if one table does not include a customer
TABLE A
Customer
--------
1
1
4
4
5
6
and
TABLE B
Customer
--------
4
5
5
5
6
6
7
7
7
into a summary table
SUMMARY
Customer CountA CountB
-----------------------
1 2 0
4 2 1
5 1 3
6 1 2
7 0 3
Any ideas on how to do something like this?
SELECT customer,SUM(source = 'a') cnta, SUM(source = 'b') cntb FROM
(
SELECT 'a' source,customer FROM customer_a
UNION ALL
SELECT 'b',customer FROM customer_b
) n
GROUP
BY customer;
I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.