I have three tables and they are the following
User Table
+---------+-----------+--------+
| user_id | user_name | branch |
+---------+-----------+--------+
| 1 | John | 1 |
| 2 | Jim | 2 |
| 3 | Jern | 3 |
| 4 | Jack | 1 |
| 5 | Jery | 2 |
| 6 | Tom | 3 |
| 7 | Sona | 1 |
| 8 | Tina | 3 |
+---------+-----------+--------+
Branch Table
+-----------+----------------+
| branch_id | branch_name |
+-----------+----------------+
| 1 | IT |
| 2 | SALES |
| 3 | Administration |
+-----------+----------------+
Enquiry Table
+------------+---------------+---------+
| enquiry_id | enquiry_name | user_id |
+------------+---------------+---------+
| 1 | enqury_test1 | 1 |
| 2 | enqury_test2 | 2 |
| 3 | enqury_test3 | 1 |
| 4 | enqury_test4 | 3 |
| 5 | enqury_test5 | 2 |
| 6 | enqury_test6 | 5 |
| 7 | enqury_test7 | 1 |
| 8 | enqury_test8 | 2 |
| 9 | enqury_test9 | 4 |
| 10 | enqury_test10 | 6 |
| 11 | enqury_test11 | 2 |
| 12 | enqury_test12 | 7 |
+------------+---------------+---------+
From the above tables its clear that, each branch contains a number of users.
These users post multiple enquiries.
I need to get the total number of enquiries in each branch as
branch id => number of enquiries
I have tried various queries. But i couldn't get the result. Any one can help?
I am using MySQL and i need a single query to do this.
Thanks in advance
You need count and group by
select
b.branch_id,
count(e.user_id) as `total_enq`
from Branch b
left join User u on u.branch = b.branch_id
left join Enquiry e on e.user_id = u.user_id
group by b.branch_id
The query you have to perform to get you desired result is like this :-
$query = "SELECT u.branch, COUNT(u.user_id) AS `total_enquires`
FROM enquiry e INNER JOIN user u ON e.user_id = u.user_id
GROUP BY u.branch"
This will help you,and i think you don't need to join branch table as user table already contain branch_id.
This is the query
SELECT `branch`,`branch_name`,count(`user`.`user_id`),count(`enquiry_id`) FROM `user` inner join `branch` on `user`.`branch`=`branch`.`branch_id` inner join `enquiry` on `user`.`user_id`=`enquiry`.`user_id` group by `branch`
try it here
http://sqlfiddle.com/#!9/cf3eb/1
SELECT
bt.branch_id
,COUNT(enquiry_id) AS total_enquiry
FROM
enquiry_table et
INNER JOIN user_table ut on ut.user_id = et.user_id
INNER JOIN branch_table bt ON bt.branch_id = ut.branch
WHERE
1=1
GROUP BY
bt.branch_id
you can try this
Related
I have 3 tables
Table#1: groups
|---------------|---------------|
| id_group | Name |
|-------------------------------|
| 1 | Group 1 |
--------------------------------|
| 2 | Group 2 |
--------------------------------|
| 3 | Group 3 |
--------------------------------|
Table#2: jobs
|---------------|---------------|----------------------|
| id_job | id_group | name_job |
|---------------|--------------------------------------|
| 1 | 1 | name_1 |
-------------------------------------------------------|
| 2 | 1 | name_2 |
-------------------------------------------------------|
| 3 | 2 | name_3 |
-------------------------------------------------------|
| 4 | 3 | name_4 |
-------------------------------------------------------|
| 5 | 3 | name_5 |
-------------------------------------------------------|
| 6 | 3 | name_6 |
-------------------------------------------------------|
Table#3: users
|---------------|---------------|
| user | id_job |
|-------------------------------|
| A | 1 |
--------------------------------|
| B | 1 |
--------------------------------|
| C | 4 |
--------------------------------|
| D | 6 |
--------------------------------|
If I were an user (e.g C), the result I'd look for is a page where there is a table with 2 columns (USERS | JOB).
The column USERS should be filled in with all the users who share the same group. The column JOB should be filled in with all name_job of the specific group group as follows:
LOGIN: C
|---------------|---------------|
| user | job |
|-------------------------------|
| C | name_4, |
| | name_5, |
| | name_6 |
--------------------------------|
| D | name_4, |
| | name_5, |
| | name_6 |
--------------------------------|
I tried with this code, but the result is that I can read people who share the same group with me, but I can't read the jobs.
SELECT users.* jobs.*
FROM users JOIN jobs ON users.id_job=jobs.id_job
WHERE job.id_group IN
(SELECT job.id_group
FROM users JOIN jobs ON users.id_job = jobs.id_job
WHERE users.user= '$login')
Your help is appreciated in advance.
If I follow you correctly, you want the list all jobs of the group the user belongs to. One option uses a correlated subquery and string aggregation:
select u.*,
(
select group_concat(j1.name_job order by j1.id_job)
from jobs j
inner join jobs j1 on j1.id_group = j.id_group
where j.id_job = u.id_job
) as job_names
from users u
I got to table need to combine into 1
Table 1 :
| ID | FEEDBACK_VALUE |
| 1 | EMAILS |
| 2 | WALK IN |
| 3 | SMS BLAST |
| 4 | SOCIAL MEDIA |
| 5 | NEWSPAPER |
| 6 | FAMILY & FRIEND |
| 7 | OTHERS |
Table 2 :
| ID | FEEDBACK_ID |
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 7 |
| 5 | 7 |
| 6 | 7 |
| 7 | 4 |
| 8 | 4 |
| 9 | 3 |
Table 3 :
| ID | FEEDBACK_VALUE | FEEDBACK_RECEIVE |
| 1 | EMAILS | 1 |
| 2 | WALK IN | 2 |
| 3 | SMS BLAST | 1 |
| 4 | SOCIAL MEDIA | 2 |
| 5 | NEWSPAPER | 0 |
| 6 | FAMILY & FRIEND | 0 |
| 7 | OTHERS | 3 |
From table 1 and 2, How can i get result like table 3 using mysql? Thanks
You could use a left jojn, and subquery with count group by
select t1.ID, t1.FEEDBACK_VALUE, ifnull( my_count,0) feedback_receive
from table1 t1
left join (
select FEEDBACK_ID, count(*) as my_count
from table 2
group by FEEDBACK_ID
) t on t1.ID = t.FEEDBACK_ID
Just use a subquery as shown below:
SELECT A.*, (SELECT COUNT(*) FROM TABLE2 B WHERE A.ID=B.FEEDBACK_ID) AS FEEDBACK_RECEIVE
FROM TABLE1 A;
See DEMO on SQL Fiddle
Or, if less code is your thing...
SELECT x.*
, COUNT(y.id) total
FROM table_1 x
LEFT
JOIN table_2 y
ON y.feedback_id = x.id
GROUP
BY x.id;
I need help to resolve this.
I have 4 tables :
Transactions
| id | cid | gt | rt |
| 1 | 6 | 2 | 5 |
| 2 | 6 | 9 | 7.5 |
| 3 | 6 | 3 | 9.7 |
| 4 | 3 | 3 | 7.0 |
| 5 | 3 | 7 | 6.8 |
| 6 | 9 | 4 | 2.5 |
| 7 | 9 | 2 | 5.4 |
Clients
| id | firstname | lastname | date |
| 1 | jean | moulin | 1987 |
| 2 | salah | fera | 1968
| 3 | marouan | youra | 2001 |
| 4 | amin | esa | 1963 |
| 5 | kamal | tara | 1789 |
| 6 | moad | mara | 2005 |
| 9 | safaa | dara | 2004 |
Produit A
| id | cid |
| 1 | 6 |
| 2 | 6 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 1 |
Produit B
| id | cid |
| 1 | 6 |
| 2 | 3 |
| 3 | 9 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 6 |
The result that i need is :
cid | name | date | pa | pb | gt | rt |
3 | | | | | | |
6 | | | | | | |
9 | | | | | | |
I need to select from transaction all distinct client id (pid) and select the firstname and last name (name = firstname lastname) and date from clients table and sum all values (gt) and (rt) and search in table produitA the number of products for this client by his id and the same thing for the table produitB.
What i do for this but it don't work is (suggested by Gimeniux):
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(gt) AS gt,
sum(rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
The probleme here is that gt and rt values are true for only the first client. For the second client and third and ... there is different values that are not true.
Although is hard for me to see the logic between your tables, you can use this query to get the result you desire. But i think it won't work if there are two same 'gt' or two same 'rt' values for one client.
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(distinct gt) AS gt,
sum(distinct rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
Row for pid=9 doesn't show because in the data you gave there is no client with id=9
I've been having some issues joining tables in a project I'm working on, for projects.
I have 3 tables, one for Projects, one for Customers, and one for Users. I'm trying to store the ID of the Customers and Users in the Projects table, and join them when retrieving.
+----+--------------+
| ID | CustomerName |
+----+--------------+
| 1 | Customer 1 |
| 2 | Customer 2 |
| 3 | Customer 3 |
+----+--------------+
+----+-----------+----------+
| ID | FirstName | LastName |
+----+-----------+----------+
| 1 | Bob | Belcher |
| 2 | Stirling | Archer |
| 3 | Bart | Simpson |
| 4 | Peter | Griffin |
| 5 | BoJack | Horseman |
| 6 | Eric | Cartman |
+----+-----------+----------+
+----+---------------+-----------------+-------------+-------------------+-------------------+--------------------+
| ID | ProjectNumber | ProjectCustomer | ProjectLead | ProjectElectrical | ProjectMechanical | ProjectDescription |
+----+---------------+-----------------+-------------+-------------------+-------------------+--------------------+
| 1 | 0001 | 1 | 3 | 4 | 6 | Project 1 |
| 2 | 0002 | 2 | 2 | 5 | 5 | Project 2 |
| 3 | 0003 | 3 | 1 | 6 | 4 | Project 3 |
+----+---------------+-----------------+-------------+-------------------+-------------------+--------------------+
I've been playing around with Select's all day and this is as far as I've been able to get searching SO:
select Projects.ProjectNumber, Customers.CustomerName, CONCAT_WS(' ', Users.FirstName, Users.LastName) AS ProjectLead, Projects.ProjectElectrical, Projects.ProjectMechanical, Projects.ProjectDescription FROM Projects
INNER JOIN Customers ON Projects.ProjectCustomer = Customers.ID
LEFT JOIN Users ON Projects.ProjectLead = Users.ID
Which gets me part of the way there:
+---------------+--------------+-----------------+-------------------+-------------------+--------------------+
| ProjectNumber | CustomerName | ProjectLead | ProjectElectrical | ProjectMechanical | ProjectDescription |
+---------------+--------------+-----------------+-------------------+-------------------+--------------------+
| 0001 | Customer 1 | Bart Simpson | 4 | 6 | Project 1 |
| 0002 | Customer 2 | Stirling Archer | 5 | 5 | Project 2 |
| 0003 | Customer 3 | Bob Belcher | 6 | 4 | Project 3 |
+---------------+--------------+-----------------+-------------------+-------------------+--------------------+
But for the life of me, I can't get ProjectElectrical and ProjectMechanical to do the same thing as ProjectLead. I either get duplicates of ProjectLead, or I get NULLs.
Can anyone help point me in the right direction? Do I need to completely redesign my query or am I on the right track?
I've fiddle'd it at SQL Fiddle
Thanks in advance for any and all replies!
Here is the updated query from the fiddle:
select Projects.ProjectNumber, Customers.CustomerName, CONCAT_WS(' ', Users.FirstName, Users.LastName) AS ProjectLead, CONCAT_WS(' ', u2.FirstName, u2.LastName) AS ProjectElectrical, CONCAT_WS(' ', u3.FirstName, u3.LastName) AS ProjectMechanical, Projects.ProjectDescription FROM Projects
INNER JOIN Customers ON Projects.ProjectCustomer = Customers.ID
LEFT JOIN Users ON Projects.ProjectLead = Users.ID
LEFT JOIN Users AS u2 ON Projects.ProjectElectrical = u2.ID
LEFT JOIN Users AS u3 ON Projects.ProjectMechanical = u3.ID
I have a problem with a SQL query and the resultset being returned not being what I expected.
I have these three tables that I am trying to relate.
events_detail
__________________
| ID | start_date |
| 1 | 2012-08-09 |
| 2 | 2013-02-13 |
| 3 | 2012-12-12 |
| 4 | 2013-01-21 |
| 5 | 2012-12-25 |
-------------------
where ID is the primary key
events_category_relationship
__________________________
| ID | event_id | cat_id |
| 1 | 1 | 1 |
| 2 | 2 | 4 |
| 3 | 3 | 2 |
| 4 | 4 | 2 |
| 5 | 5 | 3 |
--------------------------
where ID is primary key
events_category_detail
__________________________________
| ID | name | description |
| 1 | Europe | Kings and castles! |
| 2 | USA | Freedoms |
| 3 | China | Made in China |
| 4 | UK | Big Brother |
------------------------------------
where ID is primary key
What I need to do is grab only 1 event from each category and sorted by date of earliest appearance. So what I should expect in my result is the following
Result Set
________________________________________________________________
| e_id | start_date | c_id | category_name | category_desc |
| 1 | 2012-08-09 | 1 | Europe | Kings and castles! |
| 3 | 2012-12-12 | 2 | USA | Freedoms |
| 5 | 2012-12-25 | 3 | China | Made in China |
| 2 | 2013-02-13 | 4 | UK | Big Brother |
-----------------------------------------------------------------
My SQL query that I tried looks like this
SELECT e.id, e.start_date, c.category_name, c.category_desc
FROM events_detail e
JOIN events_category_relationship r ON r.event_id = e.id
JOIN events_category_detail c ON c.id = r.cat_id
ORDER BY date(e.start_date)
This just joins the 3 tables and returns the result in order by date. What I am stuck on is making it so that only one of each category is displayed like the desired result set above. I have tried using DISTINCT c.category_name and GROUP BY c.category_name, but none of them works.
Any help or advice will be greatly appreciated.
You will want to use a subquery to get the min(start_date) for each name and description. You will then use this result to join back to your events_details table to get the id associated with the data in the subquery:
SELECT e.id,
d.start_date,
d.name,
d.description
FROM events_detail e
INNER JOIN
(
select min(e.start_date) Start_date,
c.name,
c.description
from events_detail e
INNER JOIN events_category_relationship r
ON r.event_id = e.id
INNER JOIN events_category_detail c
ON c.id = r.cat_id
group by c.name, c.description
) d
on e.start_date = d.Start_date
ORDER BY date(e.start_date)
See SQL Fiddle with Demo