MySQL JOIN Query Show Columns with IF ELSE Selected Option - mysql

I have 2 Tables named users & selected_users. I have show the structure of tables with some dummy values below.
Table: users AS t1
+----+--------+
| id | name |
+----+--------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
+----+--------+
Table: selected_users
+----+------------+
| id | users_id |
+----+------------+
| 1 | 2 |
| 2 | 5 |
+----+------------+
Desired Result:
+-------+----------+-----------+
| t1_id | t1_name | selected |
+-------+----------+-----------+
| 1 | A | NO |
| 2 | B | YES |
| 3 | C | NO |
| 4 | D | NO |
| 5 | E | YES |
+-------+----------+-----------+
What I have done:
I have written the following script.
SELECT t1.id AS t1_id, t1.name AS t1_name, CASE WHEN t2.t1_id=t1.id THEN 'YES' ELSE 'NO' END AS selected_in_t2
FROM t1
JOIN t2
But, Its showing me multiple values for each. Please help.

I did it using this Query.
SELECT t1.id AS t1_id, t1.name AS t1_name,
CASE WHEN t2.id IS NULL THEN 'No' ELSE 'Yes' END as selected
FROM t1
LEFT JOIN t2
ON t1.id = t2.t1_id ORDER BY t1.id

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

Combining multiple JOINS(inner and Left)into one Select

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

How can i join 2 tables with option values on certain column

On table1
+-------+-------+
| unid1 | unid2 |
+-------+-------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+-------+-------+
On table 2
+---------+-------+-------+------+
| tableid | unid1 | unid2 | type |
+---------+-------+-------+------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 1 |
| 3 | 3 | 3 | 1 |
| 4 | 3 | 0 | 2 |
| 5 | 3 | 0 | 2 |
| 6 | 4 | 4 | 3 |
| 7 | 5 | 5 | 3 |
+---------+-------+-------+------+
Expected result.
+-------+-------+------+
| unid1 | unid2 | type |
+-------+-------+------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 3 | 0 | 2 |
| 3 | 0 | 2 |
+-------+-------+------+
My SQL code
select t1.*, t2.* from table1 t1
left join table2 t2 on t1.unid1 = t2.unid1 and t1.unid2 = t2.unid2 and
t2.type in (1 , 2);
My SQL query does not give the result that i wanted, how can i get the result that i want.
Your expected output implies that you want to retain all rows in table2 whose unid1 values can be found in table1. If so, then we can just inner join these two tables on the unid1 column. This assumes that unid1 is unique in table1.
SELECT t2.unid1, t2.unid2, t2.type
FROM table2 t2
INNER JOIN table1 t1
ON t2.unid1 = t1.unid1;
If I understand you correctly, you want to output values wherein the rows from Table1 matches on Table2 in both columns unid1 and unid2 and that the type must be contained with your desired values.
SELECT b.unid1, b.unid2, b.type
FROM table1 a
INNER JOIN table2 b
ON a.unid1 = b.unid1
WHERE EXISTS
(
SELECT 1
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.unid1 = t2.unid1
AND t1.unid2 = t2.unid2
WHERE t2.type in (1,2)
AND a.unid1 = t1.unid1
)
Here's a DEMO.

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 inner join where table2 child_id is like table2 id

I have two tables and as the title says I need something like a double inner join no. I have no idea if that works but I believe there should be an easy way.
What I've got is this statement which works fine:
SELECT
t1.id img_id, t1.nav_id img_nav_id, t1.name img_name, t1.img_title img_title, t1.img_text img_text,
t2.id nav_id,t2.parent_id nav_parent_id, t2.name nav_name, t2.directlink nav_directlink
FROM images t1
INNER JOIN navigation t2
ON t2.id=t1.nav_id
ORDER BY RAND() LIMIT 0,101
now t2 (navigation table) looks like this
+----+-----------+------------------+------------------+------+
| id | parent_id | name | directlink | rang |
+----+-----------+------------------+------------------+------+
| 1 | 0 | Home | home | 0 |
| 3 | 0 | Architektur | architektur | 1 |
| 7 | 0 | Design | design | 2 |
| 8 | 0 | Contact | contact | 3 |
| 11 | 3 | Surfabricaziun 5 | surfabricaziun_5 | 0 |
| 12 | 7 | Fluor | fluor | 1 |
| 13 | 7 | Maisa | maisa | 2 |
| 14 | 3 | Fuldera | fuldera | 3 |
and t1 (images table) looks like this
+-----+--------+------+----------------------+-----------+----------+
| id | nav_id | rang | name | img_title | img_text |
+-----+--------+------+----------------------+-----------+----------+
| 700 | 11 | 80 | Siedlg_aussen_26.jpg | | |
the output I get from the sql statement is:
+--------+------------+-------------+-----------+----------+--------+---------------+----------+------------------+
| img_id | img_nav_id | img_name | img_title | img_text | nav_id | nav_parent_id | nav_name | nav_directlink |
+--------+------------+-------------+-----------+----------+--------+---------------+----------+------------------+
| 625 | 11 | 07.jpg | 11 | | 11 | 3 | Surfabri | surfabricaziun_5 |
| 744 | 20 | 85.jpg | | | 20 | 7 | Test | test |
now What I want or need is: I need to get the parent nav name. So I would like one more field called nav_parent_name where t2.parent_id = t2.id and for this I've tried
SELECT
t1.id img_id, t1.nav_id img_nav_id, t1.name img_name, t1.img_title img_title, t1.img_text img_text,
t2.id nav_id,t2.parent_id nav_parent_id, t2.name nav_name, t2.directlink nav_directlink,
t2.name nav_parent_name
FROM images t1
INNER JOIN navigation t2
INNER JOIN navigation t2
ON t2.parent_id = t2.id AS nav_parent_name
ON t2.id=t1.nav_id
ORDER BY RAND() LIMIT 0,101
which isn't working. Problem: I don't know anything about join is it possible to get the result I want or do I have to write a new Sql statement which would be easy but I would love to have only one statement working for all data I need.
Thanks in advance to everyone reading for any suggetions and advices.
you have some aliasing problems and need to keep your existing join while adding a new on
SELECT
t1.id img_id, t1.nav_id img_nav_id, t1.name img_name, t1.img_title img_title, t1.img_text img_text,
t2.id nav_id,t2.parent_id nav_parent_id, t2.name nav_name, t2.directlink nav_directlink,
t3.name nav_parent_name
FROM images t1
INNER JOIN navigation t2
ON t2.id=t1.nav_id
INNER JOIN navigation t3
ON t2.parent_id = t3.id
ORDER BY RAND() LIMIT 0,101