mysql union query - php

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

Related

Comparing tables in MySQL and adding a yes/no response

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;

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

How to get posts from posts table with both known and unknown authors

I have a posts table in sql which its author column is connected to users table. I use a single sql query to get post from posts table and the author's name from users table. My posts table looks like this:
+----+-----------------+-----------------+--------+
| id | title | label | author |
+----+-----------------+-----------------+--------+
| 22 | Post 1 | post-1 | 2 |
| 24 | Post 2 | post-2 | 4 |
| 25 | Post 3 | post-3 | 4 |
| 26 | Post 4 | post-4 | 5 |
| 27 | Post 5 | post-5 | 6 |
| 28 | Post 6 | post-6 | 2 |
| 29 | Post 7 | post-7 | 2 |
| 30 | Post 8 | post-8 | 2 |
| 32 | Post 9 | post-9 | 2 |
+----+-----------------+-----------------+--------+
I use this sql query to get post title, label and its authors name and surname.
SELECT `posts`.id, `posts`.title, `posts`.label, users.id AS author, users.name AS name, users.surname AS surname
FROM `posts`
INNER JOIN users
ON users.id = posts.author
ORDER BY `date` DESC;
This works perfectly fine but it only returns posts with known authors which is id 2 because in users table I only have author id 2. Other authors (4, 5 and 6) are missing. So instead of not showing the posts with unknown authors, I want to show them null as its name and surname variable. By the way the result is;
+----+----------------+----------------+--------+------+---------+
| id | title | label | author | name | surname |
+----+----------------+----------------+--------+------+---------+
| 32 | Post 9 | post-9 | 2 | Jack | Smith |
| 30 | Post 8 | post-8 | 2 | Jack | Smith |
| 29 | Post 7 | post-7 | 2 | Jack | Smith |
| 28 | Post 6 | post-6 | 2 | Jack | Smith |
| 22 | Post 1 | post-1 | 2 | Jack | Smith |
+----+----------------+----------------+--------+------+---------+
Instead of INNER JOIN you have to use LEFT JOIN. The result of an inner join just shows entities which are represented in both tables. A left join adds a null value, when an entity is represented in the first table but not in the second.
Try to use left Join :
one more this create the foreign key constrain while refer another table value.
SELECT `posts`.id, `posts`.title, `posts`.label, posts.id AS author,
users.name AS name, users.surname AS surname
FROM `posts`
LEFT JOIN users
ON users.id = posts.author
ORDER BY `date` DESC;

Get multiple values from table

I have a table customers
| id | firstname |
| 1 | paul |
| 2 | steve |
| 3 | chris |
second table called list_customer
| id | id_customer | id_list |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
Each customer can be in x number of lists
third table called list
| id_list | color |
| 1 | #fff |
| 2 | #000 |
| 3 | #ccc |
With mysql query I want to get firstname and list colors.
Customers can be in multiple numbers of lists.
select c.firstname, group_concat(l.color) as colors
from customers c
inner join list_customer lc on lc.id_customer = c.id
inner join list l on l.id_list = lc.id_list
group by c.firstname
Try this:
SELECT c.FIRSTNAME,
l.COLOR AS ListColors
FROM CUSTOMERS c,
LIST_CUSTOMER lc,
LIST l
WHERE lc.ID_CUSTOMER = c.ID
AND l.ID_LIST = lc.ID_LIST
GROUP BY c.FIRSTNAME

Selecting random triplicates (or more) from database

I have two tables:
Students Student_Grades
V------------------------V
+----+------+ +----+------------+---------+-------+
| id | name | | id | student_id | subject | grade |
+----+------+ +----+------------+---------+-------+
| 0 | Dave | | 0 | 0 | Math | 100 |
+----+------+ +----+------------+---------+-------+
| 1 | John | | 1 | 0 | Chem | 90 |
+----+------+ +----+------------+---------+-------+
| 2 | Kate | | 2 | 0 | CompSCI | 95 |
+----+------+ +----+------------+---------+-------+
| 3 | Mimi | | 3 | 1 | ELA | 98 |
+----+------+ +----+------------+---------+-------+
| 4 | 2 | Biology | 92 |
+----+------------+---------+-------+
| 5 | 2 | Chem | 94 |
+----+------------+---------+-------+
| 6 | 2 | Math | 98 |
+----+------------+---------+-------+
| 7 | 3 | Math | 100 |
+----+------------+---------+-------+
I would like to select all subjects and grades from a random student that is enrolled in more than three subjects. (Either Dave or Kate)
Students John and Mimi would not be even considered because they are not enrolled in three subjects.
I know I can achieve this with PHP but I would like this to be done with one query to the database.
SELECT * FROM Students t JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM Students) AS x ON t.ID >= x.ID LIMIT 1
With the above query, I have selected a random student, with that I can go in and check if they have three subjects with SELECT count(subjects) FROM Students WHERE id=random_id.
If the count returned is below three, then I throw away the results and run the first query again.
How would I attempt this in one query?
This is tested and working:
SELECT *
FROM Students s
JOIN (
SELECT student_id
FROM Student_Grades
GROUP BY student_id
HAVING COUNT(*) >= 3
ORDER BY RAND()
LIMIT 1
) rs
ON rs.student_id = s.id
JOIN
Student_Grades sg
ON sg.student_id = s.id
Here's the SQL Fiddle: http://sqlfiddle.com/#!2/e5b5b/1

Categories