Comparing tables in MySQL and adding a yes/no response - php

Can someone help me tweak my SQL to get the desired result. I have a table of users, a table of cases, and a relationship table for users attached to cases.
Users Table - users
+----+-------+--------+
| id | first | last |
+----+-------+--------+
| 1 | Joe | Bloggs |
| 2 | John | Doe |
| 3 | Jane | Doe |
| 4 | Dave | Smith |
+----+-------+--------+
Case Table - cases
+----+--------+------+
| id | Case | Code |
+----+--------+------+
| 1 | Case 1 | C1 |
| 2 | Case 2 | C2 |
| 3 | Case 3 | C3 |
+----+--------+------+
Case Users Table - case_users
+----+---------+---------+
| id | case_id | user_id |
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 4 |
| 4 | 3 | 1 |
| 5 | 1 | 4 |
+----+---------+---------+
I want to query the database to return me a full list of users and return a yes or no if they are linked to case 1 and then case 2 case 3 etc.
The SQL I've got so far (selecting for Case ID 1) returns all users but is assigning 'no' to everyone:
SELECT
users.first,
users.last,
CASE WHEN case_users.case_id IS NULL THEN 'N' ELSE 'Y' END AS 'yes/no'
FROM
users
LEFT OUTER JOIN case_users ON case_users.case_id = 1
My actual result is:
+-------+--------+--------+
| First | Last | Yes/No |
+-------+--------+--------+
| Joe | Bloggs | Y |
| John | Doe | Y |
| Jane | Doe | Y |
| Dave | Smith | Y |
+-------+--------+--------+
My desired result should be:
+-------+--------+--------+
| First | Last | Yes/No |
+-------+--------+--------+
| Joe | Bloggs | Y |
| John | Doe | Y |
| Jane | Doe | N |
| Dave | Smith | Y |
+-------+--------+--------+
Can someone help me as I can't get the desired result?

You're missing the join condition from users to case_users:
SELECT
users.first,
users.last,
CASE WHEN case_users.case_id IS NULL THEN 'N' ELSE 'Y' END AS 'yes/no'
FROM
users
LEFT OUTER JOIN case_users ON users.id = case_users.user_id AND case_users.case_id = 1

You can try this one.
SELECT u.first, u.last, IF(cu.case_id,'Y','N') AS CASE_1 FROM users u
LEFT JOIN case_users cu ON u.id = cu.user_id AND cu.case_id = 1
Can use IF(cu.case_id,'Y','N')condition also instead of CASE

SELECT u.*
, CASE WHEN cu.case_id IS NULL THEN 'N' ELSE 'Y' END linked
FROM users u
JOIN cases c
LEFT
JOIN case_users cu
ON cu.user_id = u.id
AND cu.case_id = c.id
WHERE c.id = 1;

Related

Join 3 tables; select a list from specific group

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

How to add field in MySQL result?

I want add one filed to result of queries in MySQL.
this tables is my database tables :
tbl_user
id_user | username | password
-----------------------------
1 | Pooria | 123456
2 | mary | 123456
3 | july | 123456
4 | Ali | 123456
5 | shahla | 123456
tbl_category
id_category | name
--------------------
1 | BMW
2 | Toyota
3 | Benz
tbl_content
id_content | message | ContentLike
--------------------------------------
1 | Best Car | 2
2 | Best Car | 3
3 | Best Car | 4
4 | Best Car | 1
5 | Best Car | 1
tbl_user_category_content
id | id_user | id_category | id_content
----------------------------------------
1 | 2 | 2 | 4
1 | 3 | 3 | 3
1 | 4 | 3 | 5
1 | 5 | 1 | 2
tbl_user_like
id_user_like | id_user | id_content
-------------------------------------
1 | 2 | 5
1 | 2 | 3
1 | 5 | 1
1 | 4 | 2
1 | 4 | 2
My Query in Mysql :
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
and Result
id_content | message | ContentLike | username
----------------------------------------------
1 | Best Car | 2 | Pooria
2 | Best Car | 4 | mary
3 | Best Car | 3 | july
4 | Best Car | 5 | Ali
5 | Best Car | 4 | shahla
I expect Result :
I want insert a id_user and view Result:
example : insert id_user : 2 (mary)
id_content | message | ContentLike | username | Like_User
-------------------------------------------------------------------
1 | hello world1 | 4 | Pooria |
2 | hello world2 | 2 | mary |
3 | hello world3 | 2 | july | Yes
4 | hello world4 | 2 | Ali |
5 | hello world5 | 2 | shahla | Yes
add result Like_User
important:I want all entries in the database with the person who likes to show a distinct field. thanks you for help me <3
Add this in the select columns :
case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
So it would be something like this
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username, case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
I have not test it. Try it out and maybe you should change the column names in the case
Try This Query
SELECT distinct tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username,tbl_user_category_content.category
FROM tbl_user_category_content
INNER JOIN tbl_user,tbl_cat,tbl_post ON
tbl_user_category_content.id_user = tbl_user.id_user
tbl_user_category_content.id_category = tbl_cat.id_category
tbl_user_category_content.id_content = tbl_content.id_content
WHERE tbl_user.id_user='$user_id'

MySQL getting total number of enquiries for each branch

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

Complicated mySQL query in php page

If you have seen my previous requests you will see I am a TOTAL NOOB with php and mySQL. I wish I wasn't even doing this but I have been dumped in the deep end by people that have let me down. It is 1 day away from "completion" and I am left to pick up and mend the pieces of this system I have which doesn't fully function!
I have tried to explain this as logically and clearly as possible. If you need clarification please let me know and I will do my best!
Thanks in advance for any help as I am now desperate to get this thing done!
I have 3 issues!
ISSUE1
Matching Keys
(x)
TABLE1
| C_ID | C_Eth_O (x) |
+--------------------+-------------------------+
| 234 | 8 |
| 341 | 11 |
| 440 | 2 |
TABLE2
| Eth_ID (x) | C1_Eth_O |
+-----------------------+------------------------+
| 2 | Label2 |
| 8 | Label8 |
| 9 | Label9 |
| 11 | Label11 |
I need to list all the "C1_Eth_O" values in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Eth_ID" value(s) and return all the value(s) from TABLE1 where any of the selected options "C_Eth_O" = "Eth_ID". Each "C_ID" can only have one "C_Eth_O".
ISSUE2
Matching Keys
(x)
(o)
TABLE1
| C_ID (x) |
+---------------------+
| 234 |
| 341 |
| 440 |
TABLE3
| Ail_ID (o) | Ali_Label |
+-------------------------+-------------------------+
| 1 | Label1 |
| 2 | Label2 |
| 3 | Label3 |
| 4 | Label4 |
| 5 | Label5 |
| 6 | Label6 |
TABLE4
| CA_ID | C1_ID (x) | Ail1_ID (o) |
+---------------------+------------------------+------------------------+
| 1 | 234 | 1 |
| 2 | 341 | 4 |
| 3 | 341 | 6 |
| 4 | 440 | 2 |
I need to list all the "Ali_Label" values from TABLE3 in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Ail_ID" value(s) from TABLE3 and return all the value(s) from TABLE1 where any of the selected match in TABLE4 "Ali1_ID" = "Ail_ID" & "C1_ID" = "C_ID". Each "C_ID" can have multiple "Ail_ID" values
ISSUE3
Matching Keys
(x)
(o)
(-)
TABLE1
| C_ID (x) |
+---------------------+
| 234 |
| 341 |
| 440 |
TABLE3
| Ail_ID (o) | Ali_Label |
+------------------------+-------------------------+
| 1 | Label1 |
| 2 | Label2 |
| 3 | Label3 |
| 4 | Label4 |
| 5 | Label5 |
| 6 | Label6 |
TABLE5
| R_ID | C1_ID (x) | Cf1_ID (-) |
+-----------------------+-----------------------+-----------------------+
| 1 | 234 | 768 |
| 2 | 234 | 854 |
| 3 | 234 | 768 |
| 4 | 440 | 854 |
TABLE6
| CA_ID | Cf_ID (-) | Ail1_ID (o) |
+---------------------+------------------------+------------------------+
| 1 | 768 | 1 |
| 2 | 854 | 4 |
| 3 | 768 | 6 |
| 4 | 880 | 2 |
I need to list all the "Ali_Label" values from TABLE3 in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Ail_ID" value(s) from TABLE3 and return all the value(s) from TABLE1 where any of the selected "Ali_ID" = "Ail1_ID" & "C1_ID" = "C_ID" & "Cf1_ID" = "Cf_ID". Each "C_ID" can have multiple "Cf1_ID" values AND each "Cf_ID" can have multiple "Ail1_ID" values.
I have the system working returning simple queries such as
SELECT * FROM table1 WHERE C_ID = 234
but nothing with multiple tables and multiple results per C_ID!
Thanks again!
ISSUE 1 -
SELECT * FROM table1 INNER JOIN table 2 ON table1.C_Eth_O = table2.Eth_ID
ISSUE 2 -
SELECT * FROM table1 INNER JOIN table4 ON table1.C_ID = table4.C1_ID INNER JOIN table3 ON table3.Ail_ID = table4.Ail1_ID
ISSUE 3 -
SELECT * FROM table1 INNER JOIN table5 ON table1.C_ID = table5.C1_ID INNER JOIN table6 ON table5.Cf1_ID = table6.Cf_ID INNER JOIN table3 ON table3.Ail_ID = table6.Ail1_ID

mysql union query

The table that contains information about members has a structure like:
id | fname | pic | status
--------------------------------------------------
1 | john | a.jpg | 1
2 | mike | b.jpg | 1
3 | any | c.jpg | 1
4 | jacky | d.jpg | 1
Table for list of friends looks like:
myid | date | user
-------------------------------
1 | 01-01-2011 | 4
2 | 04-01-2011 | 3
I want to make a query that will as result print users from "friendlist" table that contains photos and names of that users from "members" table of both, myid (those who adding) and user (those who are added).
That table in this example will look like:
myid | myidname | myidpic | user | username | userpic | status
-----------------------------------------------------------------------------------
1 | john | a.jpg | 4 | jacky | d.jpg | 1
2 | mike | b.jpg | 3 | any | c.jpg | 1
This should do it:
SELECT
m1.id AS myid,
m1.fname AS myidname,
m1.pic AS myidpic,
m2.id AS user,
m2.fname AS username,
m2.pic AS userpic,
m1.status
FROM
members m1
INNER JOIN friends f ON m1.id = f.myid
INNER JOIN members m2 ON f.user = m2.id

Categories