Mysql group by some field non distinct - mysql

I have some task.
I need to get this table. It consist of two tables. where table_2.name not distinct.
Please help me to make this query. Thanks!
id name1 id name2
1 Alex 2 Alexander
2 Alex 3 Alexan
4 Vlad 5 Vladimir
5 Vlad 6 Vladik
From two tables.
Table_1
id name
1 Alex
2 Pit
3 Vlad
And
Table_2
id id_table_1 real_name
1 1 Alexander
2 1 Alexan
3 2 Piter
4 3 Vladimir
5 3 Vladik
my query
select table_1.name,table_2.id,table_2.real_name
from table_1 join table_2
where table_1.id = table_2.id_table_1

if all you want is to combine duplicated rows, use SELECT DISTINCT.
If you need to combine rows that are duplicate in some columns, use GROUP BY but you need to to specify what to do with the other columns. You can either omit them (by not listing them in the SELECT clause) or aggregate them (using functions like SUM, MIN, and AVG)

Related

MySQL substring to self join

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!

How to calculate count of records by 2 columns

In MySQL I have a table.
Example:
id name type
1 Thomas 2
2 Thomas 2
3 Thomas 1
4 Paul 3
5 Paul 4
6 Paul 4
I need calculate same records by 2 columns.
Result for this example should be:
name type countOfRecords
Thomas 2 2
Thomas 1 1
Paul 3 1
Paul 4 2
Could you help me with this request?
Since you want records in your result set for each name and type <name,type> pair
you need to group by name and type.
SELECT
name,
type,
COUNT(*) countOfRecords
FROM your_table
GROUP BY name,type;
Note:
Group BY <some column> would generate a result set where number of rows = number of distinct / different / unique <some column>.
Same holds for multiple columns in GROUP BY clause.
This may be right solution:
SELECT name, type, COUNT(*) as countOfRecords
FROM your_table
GROUP BY name,type;

Error for INNER JOIN with GROUP BY and WHERE clause

I've got these tables
student:
id Name
-------------------
1 john
2 carlos
3 zoya
4 arab
5 amir
and,
email:
id email student_id
--------------------------
1 a#mail.com 1
2 b#mail.com 2
3 c#mail.com 2
4 d#mail.com 3
5 e#mail.com 4
I'm using this query and it's getting Using sql error on query line 4,
SELECT * FROM student
INNER JOIN email
ON student.id = email.student_id
GROUP BY student.id
WHERE student.id = 2
I don't have much experience in SQL.
Firstly, GROUP BY always goes after the WHERE clause.
Secondly, if you are using the GROUP BY clause you should either use aggregate functions for the fields, that are not contained in it and still are present in the SELECT clause, or GROUP BY the whole pack of columns contained in the SELECT.

appending results from two sql queries with same fields

i have two sql queries which give resultset with same attributes... i want to combine these two result sets...
my first query gives
order_id frequency
-------------------------
1 5
3 7
10 2
12 3
and second query gives
order_id frequency
-------------------------
1 3
10 2
12 8
what i finally want in result is
order_id frequency
-------------------------
1 5
3 7
10 2
12 3
1 3
10 2
12 8
here union will not work as if there is are two same tuples such as pair
10 2
it should appear twice.
please suggest some mysql query;
have you tried with UNION ALL?
Use
UNION ALL
to avoid removing dups.
You need Union All
Union must have an equal number of expressions in their target lists
Select order_id, frequency from Table_A
Union All
Select order_id, frequency from Table_B

Make in clause to match all items ot any alternative?

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