N
N
Nikolaymac2016-12-08 08:08:16
MySQL
Nikolaymac, 2016-12-08 08:08:16

How to write correct SQL query?

There is a users table where there is a lot of information about the user, let's take the main
id_user first_name gender
The second visits table Contains information about user visits from certain clients.
id_visit id_user id_client
Accordingly, there are many visits of the same id_user and different clients in the visits table.
You need to select how many men gender = 1 and how many women gender = 2 among all visits for a particular id_client e.g. id_client = 12
SELECT users.gender, COUNT(*) FROM `users` LEFT JOIN vizits ON vizits.id_user = users.id_user WHERE vizits. id_client='12' AND users_hotspot.gender!=0 GROUP BY users.gender
With this query, it summarizes all id_user i.e. and if you do GROUP BY vizits.id_user then it does not collect in two columns as in the first request
sex COUNT (*)
1 45836
2 34267

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question