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
Related
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 have a query
$cartable = DB::table('requests')
->select('requests.*','crequest.*')
->leftJoin('crequest','crequest.id','=','requests.request_id')
->whereRaw("FIND_IN_SET('$userid', crequest.user_allows)")
->orderBy('forms_cartable.id','desc')->get();
my tables:
requests:
+------+-------------+-------------------+-------------------+
| id | request_name | request_priority | request_status |
+------+-------------+-------------------+-------------------+
| 1 | test1 | low | process |
| 2 | test2 | low | process |
| 3 | test3 | low | process |
+------+--------------+------------------+-------------------+
crequest
+------+-------------+----------------+--------------+
| id | request_id | users_allow | c_status |
+------+-------------+----------------+--------------+
| 3 | 1 | 12,13,15 | done |
| 4 | 1 | 12 | done |
| 5 | 1 | 13 | end |
| 6 | 2 | 42,12,35 | done |
| 7 | 2 | 47,65 | done |
| 8 | 3 | 42 | open |
+------+-------------+----------------+--------------+
I want if there was an existing current user ID(Logged
) in crequest.user_allows return request`s only without crequest detail
look like this:
(if current user id 12 )
request table result (after execute query)
+------+-------------+-------------------+-------------------+
| id | request_name | request_priority | request_status |
+------+-------------+-------------------+-------------------+
| 1 | test1 | low | process |
| 2 | test2 | low | process |
+------+--------------+------------------+-------------------+
but query result all record match in table crequest i need only requests rows if have match in crequest table
I don't know php but you can achieve it in MySql in so many ways. These are 3 ways among them..
Method 1:
SELECT DISINCT R.*
FROM requests R
INNER JOIN crequest CR ON R.id = CR.request_id
WHERE FIND_IN_SET ('12', CR.users_allow);
Method 2:
SELECT *
FROM requests R
WHERE EXISTS (SELECT 1 FROM crequest CR WHERE R.id = CR.request_id
AND FIND_IN_SET ('12', CR.users_allow));
Method 3:
SELECT *
FROM requests R
WHERE R.id in (SELECT request_id FROM crequest WHERE FIND_IN_SET ('12', CR.users_allow))
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
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
In MySQL I need to get artist with highest points by location. Please help.
user_location_tbl (user location table)
|--------------------------------------|
| countryLat | countryLong | userid |
|--------------------------------------|
| 31.695766 | 54.624023 | 1 |
| 20.593684 | 78.96288 | 2 |
| 20.593684 | 78.96288 | 3 |
| 20.593684 | 78.96288 | 4 |
| 31.695766 | 54.624023 | 5 |
|--------------------------------------|
fans_table (to view which artist has which user as a fan)
|----------------------|----------
| artist_id | user_id | Points |
|----------------------|----------
| 1 | 1 | 20 |
| 1 | 2 | 30 |
| 2 | 1 | 40 |
| 2 | 3 | 40 |
| 3 | 1 | 60 |
|----------------------|----------
artist_table (list of artist)
|-------------------|
| artistid | name |
|-------------------|
| 1 | raja |
| 2 | sekar |
| 3 | thomas |
|-------------------|
I need to get location wise which artist is having highest point but I can't do it in a single query. If I put sum(points) and group by countryLat, countryLong, artistid, I get the following result...
|---------------------------------|
| 100 | 20.593684 | 78.96288 | 1 |
| 50 | 20.593684 | 78.96288 | 2 |
| 100 | 31.695766 | 54.624023 | 3 |
| 90 | 31.695766 | 54.624023 | 1 |
|---------------------------------|
...but I only need the artist with highest point on that location as in the example below...
|---------------------------------|
| 100 | 20.593684 | 78.96288 | 1 |
| 100 | 31.695766 | 54.624023 | 3 |
|---------------------------------|
SQL
SELECT Sum(c.tot_points),
a.artist_id
FROM `fans_table` AS a
INNER JOIN `user_location_tbl` AS b
ON a.user_id = b.user_id
INNER JOIN artist_table AS c
ON a.artist_id = c.id
GROUP BY b.countrylat,
b.countrylong,
a.artist_id
Above is my query
So assuming I understand your question right you want to get which artist has the most points for each location so this would be the process to figure it out manually:
// First we add up all the points for each artist in each location:
|----------------------------------------------|
| Points | countryLat | countryLong | artistid |
|----------------------------------------------|
| 30 | 20.593684 | 78.96288 | 1 |
| 40 | 20.593684 | 78.96288 | 2 |
| 0 | 20.593684 | 78.96288 | 3 |
| 20 | 31.695766 | 54.624023 | 1 |
| 40 | 31.695766 | 54.624023 | 2 |
| 60 | 31.695766 | 54.624023 | 3 |
|----------------------------------------------|
// Then we get the max for each location
|----------------------------------------------|
| Points | countryLat | countryLong | artistid |
|----------------------------------------------|
| 40 | 20.593684 | 78.96288 | 2 |
| 60 | 31.695766 | 54.624023 | 3 |
|----------------------------------------------|
The query to do this is as follows
SELECT artist_id, ul.countryLat, ul.countryLong, max_points
FROM fans_table f
JOIN user_location_tbl ul
ON ul.userid = f.user_id
JOIN (
SELECT countryLat, countryLong, MAX(f.total_points) max_points
FROM
(SELECT artist_id, countryLat, countryLong, SUM(Points) as total_points
FROM fans_table f
JOIN user_location_tbl ul
ON ul.userid = f.user_id
GROUP BY artist_id, countryLat, countryLong) f
GROUP BY countryLat, countryLong) p
ON p.countryLat = ul.countryLat
AND p.countryLong = ul.countryLong
GROUP BY f.artist_id, ul.countryLat, ul.countryLong
HAVING p.max_points = SUM(Points)
Making an assumption that the points in an area will be unique (ie, either there won't be 2 equally popular artists in an area, or if there are you want them both) then something like the following will do it
SELECT Sub3.artist_id, Sub3.countryLat, Sub3.countryLong, Sub2.MaxArtistLocalPoints
FROM
(
SELECT a.artist_id, countryLat, countryLong, SUM(a.Points) AS ArtistLocalPoints
FROM `fans_table` AS a
INNER JOIN `user_location_tbl` AS b ON a.user_id=b.user_id
INNER JOIN artist_table AS c ON a.artist_id=c.id
GROUP BY a.artist_id, countryLat, countryLong
) Sub3
INNER JOIN
(
SELECT countryLat, countryLong, MAX(ArtistLocalPoints) AS MaxArtistLocalPoints
FROM
(
SELECT a.artist_id, countryLat, countryLong, SUM(a.Points) AS ArtistLocalPoints
FROM `fans_table` AS a
INNER JOIN `user_location_tbl` AS b ON a.user_id=b.user_id
INNER JOIN artist_table AS c ON a.artist_id=c.id
GROUP BY a.artist_id, countryLat, countryLong
) Sub1
) Sub2
ON Sub3.countryLat = Sub2.countryLat
AND Sub3.countryLong = Sub2.countryLong
AND Sub3.ArtistLocalPoints = Sub2.MaxArtistLocalPoints