Combining multiple JOINS(inner and Left)into one Select - mysql

My problem is that i have 4 tables, that i want to combine. Table1 has Tickets, Table2 with users, Table 3 with admins and Table4 with files. My Select was woring great when i had only 3 tables, without files, my select back then was something like this
SELECT
Table1.TicketNumber,
Count(Table1.TicketNumber) as "number of tickets",
Table2.UserName,
Table3.AdminName
FROM Table1
inner join
table2 on table1.ID_U=table2.ID
inner join
table3 on table1.ID_A=table3.ID
GROUP BY Table1.TicketNumber
Then i decided to add to my select another table(Table4), from which i would sum number of files for corresponding Ticket, and my Select is something like this:
SELECT
Table1.TicketNumber ,
Table1.count,
Table4.count
FROM
( SELECT TicketNumber,
count(*) AS count
FROM Table1
GROUP BY TicketNumber
)Table1
LEFT JOIN
( SELECT TickerNumber,
count(*) as count
FROM Table2
GROUP BY TicketNumber
) Table2 ON Table1.Name=Table2.Name
My problem is when i try to in somehow merge these two selects to get all that i want, i get syntax error in my INNER JOIN that Table1.ID_U doesnt exist.
Here is simplified struct of my Tables
Table1 Table 2/3
+----+--------------+------+------+----------+ +----+----------------+
| ID | TicketNumber | ID_U | ID_A | SomeData | | ID | User/Admin Name|
+----+--------------+------+------+----------+ +----+----------------+
| 0 | T001 | 1 | 1 | blah | | 0 | Name |
| 1 | T002 | 2 | 3 | blah | | 1 | Name |
| 2 | T002 | 2 | 3 | blah | | 2 | Name |
| 3 | T003 | 2 | 2 | blah | | 3 | Name |
| 4 | T004 | 3 | 1 | blah | | 4 | Name |
+----+--------------+------+------+----------+ +----+----------------+
My Table4
+----+------------+----------+
| ID | TicketName | FileName |
+----+------------+----------+
| 0 | T002 | Name |
| 1 | T002 | Name |
| 2 | T003 | Name |
| 3 | T004 | Name |
| 4 | T007 | Name |
+----+------------+----------+
My goal is to reach Select that looks like this
+----+--------------+----------------+--------------+----------+-----------+
| ID | TicketNumber | HowManyTickets | HowManyFiles | User | Admin |
+----+--------------+----------------+--------------+----------+-----------+
| 0 | T001 | 1 | 0 | UserName | AdminName |
| 1 | T002 | 2 | 2 | UserName | AdminName |
| 2 | T003 | 1 | 1 | UserName | AdminName |
| 3 | T004 | 5 | 1 | UserName | AdminName |
+----+--------------+----------------+--------------+----------+-----------+
Unfortunaly all i am capable of doing is either getting
TicketNumber, HowManyTickets, HowManyFiles,
or
TicketNumber, HowManyTickets, User, Admin

It seems your result ID is generated since its not anymore the same to your TicketID.
Here's my suggestion, using row_number() to generate the ID, then use sum() aggregation function to group your ticketNumber. left join will give you 0 result on your Ticket T001.
select row_number() over (order by t1.TicketNumber) as ID
, t1.TicketNumber
, sum(case when coalesce(t1.TicketNumber, '') = '' then 0 else 1 end) as HowManyTickets
, coalesce(t4.numFiles, 0) as HowManyFiles
, t2.Name as USER
, t3.Name as Admin
from table1 t1
left join table2 t2 on t2.ID = t1.ID_U
left join table3 t3 on t3.ID = t1.ID_A
left join
(select count(1) as numFiles, TicketNumber from table4 group by TicketNumber) t4 on t4.TicketNumber = t1.TicketNumber
group by t1.TicketNumber, t4.numFiles

Related

Select count of same entries in different tables

I got 3 tables
| ID | Name |
|:---- |:------:|
| 1 | Brie |
| 2 | Ray |
| 3 | James |
Table2
| ID | Q_id | Q_no | ans |
|:---- |:------:| -----:|----:|
| 1 | 2304. | 1 | A |
| 1 | 2304. | 2 | A |
| 1 | 2305. | 1 | C |
| 2 | 2304. | 2 | A |
| 2 | 2305. | 1 | C |
| 3 | 2304. | 1 | A |
| 3 | 2305. | 2 | D |
Table3
| Q_id | Q_no | correct_ans |
|:------:| -----:|------------:|
| 2304. | 1 | A |
| 2304. | 2 | B |
| 2305. | 1 | C |
| 2305. | 2 | D |
I need to print a table with ID, name and count of ans in table 2 where ans matches with correct answer from table 3
| ID | Name | ans_count |
|:---- |:------:| ----------:|
| 1 | Brie | 2 |
| 2 | Ray | 1 |
| 3 | James | 2 |
Select t1.ID, Name, count(t2.ans) as ans_count
from Table1 t1
join Table2 t2 on t1.ID=t2.ID
join Table3 t3 on t2.Q_id=t3.Q_id
where t2.ans=t3.correct.ans and t2.q_no=t3.q_no
group by t1.ID
order by t1.ID
Where am I doing it wrong? Sorry I am new to SQL.
You should always link the tables, with all colums that match
AND the GROUP By should contain all columns that have no aggregation function
SELECT t1.ID,t1.`Name`,COUNT(*) as correct_answers
FROM table1 t1
JOIN table2 t2 ON t1.ID = t2.ID
JOIN table3 t3 ON t2.`Q_id` = t3.`Q_id` AND t2.`Q_no` = t3.`Q_no`
WHERE t3.`correct_ans` = t2.`ans`
GROUP BY t1.ID,t1.`Name`
order by t1.ID
ID
Name
correct_answers
1
Brie
2
2
Ray
1
3
James
2
fiddle
Its still not clear what you are after, but this corrects the obvious issue with the query.
Select t1.ID, Name, count(*) as ans_count
from Table1 t1
join Table2 t2 on t1.ID=t2.ID
join Table3 t3 on t2.Q_id=t3.Q_id
where t2.ans=t3.correct.ans
group by t1.ID, t1.Name ///<---------------
order by t1.ID
I think you need to group by Id and Name

Counting data from two tables and grouping it all by name that exists in both tables

I want to merge two tables that I have, both of them have the same name in them. Going to use it so corresponding ticket number can have few requests, and also few files in it
Table1 that is my main table Table 2 that I want to join
+----+------+-------+-------+ +----+------+-------+
| ID | Name | Info1 | Info2 | | ID | Name | Info1 |
+----+------+-------+-------+ +----+------+-------+
| 0 | L01 | blah | blah | | 0 | L01 | blah |
| 1 | L02 | blah2 | blah2 | | 1 | L01 | blah |
| 2 | L02 | blah3 | blah3 | | 2 | L03 | blah |
| 3 | L03 | Blah3 | blah3 | | 3 | L04 | blah |
| 4 | L04 | Blah4 | blah4 | | 4 | L04 | blah |
+----+------+-------+-------+ +----+------+-------+
Then I use select to group it like this
+------+-------------+
| Name | Count(Name) |
+------+-------------+
| L01 | 1 |
| L02 | 2 |
| L03 | 1 |
| L04 | 1 |
+------+-------------+
And my goal is to make something like this
+------+-------------+--------------------+
| Name | Count(Name) | Count(table2.Name) |
+------+-------------+--------------------+
| L01 | 1 | 2 |
| L02 | 2 | 0 |
| L03 | 1 | 1 |
| L04 | 1 | 2 |
+------+-------------+--------------------+
any suggestions? code I tried so far on my existing project, `NR_REKLAMACJI is my Name from Table 1, this code was working when I only counted the number of Names in this particular table:
SELECT
`NR_REKLAMACJI`,
COUNT(NR_REKLAMACJI) AS 'ilosc reklamowanego towaru',
CONCAT(klienci.IMIE, ' ', klienci.NAZWISKO) AS 'Klient',
CONCAT(users.IMIE, ' ', users.NAZWISKO) AS 'Osoba zajmująca się',
DOK_FV,
klienci.NAZWA_FIRMY,
DATA
FROM
`rtransportowa`
INNER JOIN klienci ON ID_R = klienci.ID_KLIENTA
INNER JOIN users ON ID_U = users.ID_USER
group by
NR_REKLAMACJI
You can join two subqueries result and construct a result as you want on basis of a left-join:
SELECT t1.`Name` as `Name`, t1.count AS `count(Name)`, IFNULL(t2.count, 0) AS `count(table2.Name)`
FROM (SELECT `Name`, count(*) AS count FROM tbl1 GROUP BY `Name`) t1
LEFT JOIN
(SELECT `Name`, count(*) AS count FROM tbl2 GROUP BY `Name`) t2 ON t1.`Name` = t2.`Name`;
You can use joining two table as one table
select tb1.Name,
CASE WHEN tb1_Count_Name IS NULL THEN '0' ELSE tb1_Count_Name END AS tb1_Count_Name,
CASE WHEN tb2_Count_Name IS NULL THEN '0'ELSE tb2_Count_Name END AS tb2_Count_Name from
(select DISTINCT Name as Name,Count(Name) as tb1_Count_Name from [dbo].[Table_1] as t1 group by t1.Name) as tb1 left join
(select DISTINCT Name as Name ,Count(Name) as tb2_Count_Name from [dbo].[Table_2]as t2 group by t2.Name )as tb2 on tb1.Name=tb2.Name

combine information from two tables into one table

I need to combine information from two tables into one table, the following table is
table 1
+----+---------------+---------+
|id_k| name | value |
+----+---------------+---------+
| 1 | enak | 4 |
| 2 | nor | 3 |
+----+---------------+---------+
table 2
+------+------+---------+
| id_d | id_k | feel |
+------+------+---------+
| 1 | 1 | good |
| 2 | 1 | better |
| 3 | 1 | verygood|
+------+------+---------+
result should be
+------+------+-------+------------------------+
| id_d | name | value | feel |
+------+------+-------+------------------------+
| 1 | enak | 4 | good, better, verygood |
| 2 | nor | 3 | |
+------+------+-------+------------------------+
this is my code [not worked]
select k.name, k.value, s.feel
from table1 as k
left join table2 as s on s.id_k=k.id_k
http://sqlfiddle.com/#!9/3a7564/1
SELECT t1.id_k,
t1.`name`,
t1.`value`,
GROUP_CONCAT(t2.feel) AS feel
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id_k = t2.id_k
GROUP BY t1.id_k
You could use the gorup_concat function to concatinate the values from table2 to a coma-delimited string in the result:
SELECT table1.id_k, name, value, GROUP_CONCAT(feel SEPARATOR ', ') AS feel
FROM table1
JOIN table2 ON table1.id_k = table2.id_k
GROUP BY table1.id_k

Mysql no exist condition

I have 3 tables:
T1 = common_member
+---------------+---------------+----------------+
| uid | username | etc |
+---------------+---------------+----------------+
| 1 | hehe | |
| 2 | crazy | |
| 3 | sence | |
| 4 | jjjj | |
+------------------------------------------------+
T2 = home_friend
+---------------+---------------+----------------+
| uid | fuid | fusername |
+---------------+---------------+----------------+
| 1 | 2 | crazy |
| 1 | 3 | sence |
| 2 | 1 | hehe |
| 3 | 1 | hehe |
+------------------------------------------------+
T3 = game_table
+--------------+----------------+
| uid | etc |
+--------------+----------------+
| 1 | |
| 2 | |
| 4 | |
+-------------------------------+
I would like to query data like condition as below.
Select t1.username AS username, t1.uid AS uid FROM commom_member t1 WHERE home_friend t2 fuid = $_GET['uid'] when the t2.fuid does not exist in game_table t3.
Since I use third party software, it was not allowed do 2 select in 1 query for safety issue, so I search stackoverflow and google, I try the code below:
$queryFriendList = DB::query("SELECT * FROM ".DB::table('common_member')." t1 LEFT JOIN ".DB::table('home_friend')." t2 ON (t2.fuid='$_G[uid]') LEFT JOIN ".DB::table('game_table')." t3 ON (t2.uid = t3.uid) WHERE t3.uid is NULL");
But the code cant work properly, it will show all list exist in t3 twice.
Let's say $_G[uid] = 1;
The out put should be
+---------+---------------+
| uid | username |
+---------+---------------+
| 3 | sence |
+-------------------------+
because uid 3 in T2 is uid 1's friend but does not exist in T3.
any good suggestion for this query?

MySQL: Get the totals from two columns organised by category

Considering the following tables in a MYSQL database:
table1:
+------+-----------+----------------------+
| id | atual | category_id | user |
+------+-----------+--------------|-------+
| 1 | 100 | 1 | 1 |
| 2 | 150 | 2 | 1 |
| 3 | 50 | 1 | 2 |
+------+-----------+--------------|-------+
table2:
+------+-----------+----------------------+
| id | budget | category_id | user |
+------+-----------+--------------|-------+
| 1 | 100 | 2 | 1 |
| 2 | 150 | 1 | 2 |
| 3 | 50 | 1 | 1 |
+------+-----------+--------------|-------+
table3:
+------+-----------+
| id | name |
+------+-----------+
| 1 | one |
| 2 | two |
| 3 | three |
+------+-----------+
I want to calculate the totals for 'atual' and 'budget' given in tables 1 and 2 for a given user (1 in my example), organized by category name:
I tried the following query, which is giving me the totals for atual and budget regardless of the categories:
SELECT table2.id, table3.name AS name_category, SUM( budget ) ,
(SELECT SUM( atual) FROM table1 WHERE user =1)
FROM table2 INNER JOIN table3
ON table2.category_id=table3.id
Here is a method:
select t3.id, t3.name, sum(actual) as actual, sum(budget) as budget
from ((select category_id, sum(actual) as actual, NULL as budget
from table1
where user = 1
group by category_id
) union all
(select category_id, NULL as actual, sum(budget) as budget
from table2
where user = 1
group by category_id
)
) ab join
table3 t3
on ab.category_id = t3.id
group by t3.id, t3.name;