How do I count rows in MySQL? - mysql

I know just about the basic usage of COUNT(*) and I wonder if I can use it or some other function to get the following result.
I have a table with people and the products they have purchased (product_id_). I have second table which maps each product_code to a single product_category.
Using a simple SELECT I can combine both tables to get:
first last product_code product_category
John BGood 100 Food
John BGood 29 Beverage
John BGood 30 Beverage
Rita Black 25 Fashion
Betty Rock 36 Electronics
Betty Rock 72 Food
Betty Rock 100 Food
Betty Rock 36 Electronics
But what I would like is to count for each person the number of products it purchased from each category. product_category is an enum with 5 possible values (the four above and Other). I would like to get a table like:
first last product_category count
John BGood Food 1
John BGood Beverage 2
John BGood Fashion 0
John BGood Electronics 0
John BGood Other 0
Betty ...

SELECT first, last, product_category, COUNT(product_code)
FROM <table>
ORDER BY last, first
GROUP BY first, last, product_category

Try this query
SELECT first, last, product_category, count(product_category)
FROM <table_name>
GROUP BY product_category

Append GROUP BY person_id, product_category to your SELECT.

Related

Ignore the first order with respect to order id with same name

I want to query the database:
If two orders in a row are the same then ignore the second one. I used Distinct but I have many orders with the same name with different products so I want to ignore the next order the row with the same name in the row
order_id customer_name product_name
234 Jhon Mango
235 Jhon Mango
236 Marvel tomato
237 Paul Potato
238 Jhon Banana

How to merge same Ids data with mysql?

I have records like this after applying joins
id name item
1 bil Books
2 mike Table
2 mike chair
3 josh pen
3 josh pencil
4 peter copy
But, I want them to look like this
id name item
1 bil Books
2 mike Table,chair
3 josh pen, pencil
4 peter copy
How to achieve this?
Use group by and group_concat():
select id, name, group_concat(item) items
from mytable
group by id, name

Join Predicates And Comparison Operators

In an attempt to better understand how SQL joins work, I've run into difficulty understanding the result set returned from a query -- The tables I'm using are:
Employees
Id Name Salary Gender City
1 Sam 2500 Male London
5 Todd 3100 Male Toronto
3 John 4500 Male New York
6 Jack 7000 Male Shangri La
4 Sara 5500 Female Tokyo
2 Pam 6500 Female Sydney
and Gender:
ID Gender
1 Male
2 Female
3 Unknown
The first query returns all the columns from an inner join on the Gender column -
SELECT *
FROM Employees
INNER JOIN Gender
ON Employees.Gender = Gender.Gender
The returned result -
Id Name Salary Gender City ID Gender
1 Sam 2500 Male London 1 Male
5 Todd 3100 Male Toronto 1 Male
3 John 4500 Male New York 1 Male
6 Jack 7000 Male Shangri La 1 Male
4 Sara 5500 Female Tokyo 2 Female
2 Pam 6500 Female Sydney 2 Female
Which is pretty much what I expected. However when I changed the comparison operator -
SELECT *
FROM Employees
INNER JOIN Gender
ON Employees.Gender != Gender.Gender
What I originally thought would return an empty set, returned this -
Id Name Salary Gender City ID Gender
4 Sara 5500 Female Tokyo 1 Male
2 Pam 6500 Female Sydney 1 Male
1 Sam 2500 Male London 2 Female
5 Todd 3100 Male Toronto 2 Female
3 John 4500 Male New York 2 Female
6 Jack 7000 Male Shangri La 2 Female
1 Sam 2500 Male London 3 Unknown
5 Todd 3100 Male Toronto 3 Unknown
3 John 4500 Male New York 3 Unknown
6 Jack 7000 Male Shangri La 3 Unknown
4 Sara 5500 Female Tokyo 3 Unknown
2 Pam 6500 Female Sydney 3 Unknown
While I can kinda see how the not-equals(!=) operator would return this result it begs the question of what type of comparisons are useful in join predicates and which aren't - does the type of join [inner, right, left...] impact the returned result adversely or can the join type and comparison be resolved to actionable behavior(in other words does it always have to be ==)? Also if there are any sources out there that could help me, that would be great. Thanks.
An inner join is a subset of the Cartesian product of both tables. That is, every row in one table is paired with every row of the other table.
So, if one table has n rows and the other m rows, then the Cartesian product has n * m rows.
The on clause filters these rows. Equality is the most common filter and such joins are often called equi-joins. They are offer the most optimization opportunities and are typically the most efficient.
(Outer joins are similar but have a mechanism to include unmatched rows.)
Normally, join predicates contain at least one equality comparison (although this is not necessary). Other comparisons -- including subqueries using exists/in -- are allowed and often useful.
Any decent documentation or tutorial or book should be able to explain this.
In your case, often not exists is the intention. To find employees whose gender is not in the reference table:
SELECT e.*
FROM Employees e
WHERE NOT EXISTS (SELECT 1 FROM Gender g WHERE e.Gender = g.Gender)
Of course, such a query would be unnecessary if you used the primary key to reference the table and included proper foreign key declarations.

prediction on rowwise data or progressive data

I am working on employee attrition analysis with a table having rowwise data for a (employee like Id, name, Date_Join Date_Relieving Dept Role etc)
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11 (ie she is available)
I can run regression on this data to find the beta coefficient
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11
But I've seen other approach too.. where employee have multiple entries depending on their difference between joining date and current month or relieving month(say Employee A joined in Jan and Left in Dec so he'll have 12 entries updating corresponding columns like experience and marriage etc)
eID eName Dept Married Experience
123 John Doe HR No 0
123 John Doe HR No 1
123 John Doe HR Yes 2
123 John Doe HR Yes 3
can someone tell what differentiate two approaches.. and what is the outcome of this second approach.

Calculate sum from three tables

I have 3 mysql tables:
Client_courses
Client_courses_id Client_id Course_id
1 1 2
2 1 3
3 2 1
Client
Client_id Name
1 Tom
2 John
Courses
Course_id Name Price
1 Microsofr 100
2 Programming 250
3 Leadership 300
I need to calculate how much every client spent money on courses. For example: Tom spent 550 (250+300), John spent 100. And I am confused how to do it.
SELECT SUM(c.Price), cl.Name
FROM Client cl
INNER JOIN Client_courses clc ON cl.Client_id=clc.Client_id
INNER JOIN Courses cs ON clc.Course_id=cs.Course_id
GROUP BY cl.Name