Mysql no exist condition - mysql

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?

Related

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

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

group Items by column and order by other column

I have table as below , I want to take latest rating for the client
basically user whenever updates rating, count will be incremented and a entry will be made in table. Table goes as below
-----------------------------------------------------
|_id| name | client_id | user_id | rating | count |
-----------------------------------------------------
|1 | Four | 1 | 1 | 4 | 1 |
|2 | three | 1 | 1 | 3 | 2 |
|3 | two | 1 | 1 | 2 | 3 |
|4 | five | 1 | 1 | 5 | 4 |
|5 | two | 1 | 2 | 2 | 1 |
|6 | three | 1 | 2 | 3 | 2 |
|7 | two | 2 | 1 | 2 | 1 |
|8 | three | 2 | 1 | 3 | 2 |
-----------------------------------------------------
For rating of client_id 1 I want out put like
-----------------------------------------------------
|_id| name | client_id | user_id | rating | count |
-----------------------------------------------------
|4 | five | 1 | 1 | 5 | 4 |
|6 | three | 1 | 2 | 3 | 2 |
-----------------------------------------------------
so far I tried SELECT * FROM test
where client_id = 1 group by client_id order by count desc;
but not getting expected result, any help??
You can use left join on the same table as
select t1.* from test t1
left join test t2 on t1.user_id = t2.user_id
and t1.client_id = t2.client_id
and t1._id < t2._id
where
t2._id is null
and t1.client_id = 1
order by t1.`count` desc;
Using un-correlated subquery you may do as
select t1.* from test t1
join (
select max(_id) as _id,
client_id,
user_id
from test
where client_id = 1
group by client_id,user_id
)t2
on t1._id = t2._id
and t1.client_id = t2.client_id
order by t1.`count` desc;
UPDATE : From the comment how to join another table into above , for this here is an example
mysql> select * from users ;
+------+------+
| _id | name |
+------+------+
| 1 | AAA |
| 2 | BBB |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from test ;
+------+-------+-----------+---------+--------+-------+
| _id | name | client_id | user_id | rating | count |
+------+-------+-----------+---------+--------+-------+
| 1 | four | 1 | 1 | 4 | 1 |
| 2 | three | 1 | 1 | 3 | 2 |
| 3 | two | 1 | 1 | 2 | 3 |
| 4 | five | 1 | 1 | 5 | 4 |
| 5 | two | 1 | 2 | 2 | 1 |
| 6 | three | 1 | 2 | 3 | 2 |
| 7 | two | 2 | 1 | 2 | 1 |
| 8 | three | 2 | 1 | 3 | 2 |
+------+-------+-----------+---------+--------+-------+
select t1.*,u.name from test t1
join users u on u._id = t1.user_id
left join test t2 on t1.user_id = t2.user_id
and t1.client_id = t2.client_id
and t1._id < t2._id
where
t2._id is null
and t1.client_id = 1
order by t1.`count` desc;
Will give you
+------+-------+-----------+---------+--------+-------+------+
| _id | name | client_id | user_id | rating | count | name |
+------+-------+-----------+---------+--------+-------+------+
| 4 | five | 1 | 1 | 5 | 4 | AAA |
| 6 | three | 1 | 2 | 3 | 2 | BBB |
+------+-------+-----------+---------+--------+-------+------+
Note that the join to users table is inner join and this will require all the user to be preset in users table which are in test table
If some users are missing in the users table then use left join this will have null values for the data selected from users table.
You may try something like
select _id, name, client_id, user_id, rating, max(count)
from clients
group by client_id
Try it
SELECT * FROM test
where client_id = 1
group by user_id
order by count desc

How to fetch data from two tables using mysql query with some conditions applied to the second table?

I want to generate a result from a MySql query with below requirement.
Table 1 :
---------------
| nid | type |
---------------
| 1 | forum |
| 2 | forum |
| 3 | forum |
| 4 | forum |
---------------
Table 2
-----------------------
| nid | cid | created |
-----------------------
| 1 | 32 | 123456 |
| 2 | 65 | 123457 |
| 4 | 67 | 123458 |
| 1 | 61 | 123491 |
| 1 | 78 | 123497 |
| 2 | 23 | 123498 |
| 1 | 12 | 123698 |
| 4 | 54 | 132365 |
| 4 | 81 | 135698 |
| 1 | 30 | 168965 |
-----------------------
Now i require result like below. (Condition : I need the nid from first table, smallest cid for the corresponding nid in second table WHERE type = 'forum')
--------------
| nid | cid |
--------------
| 1 | 12 |
| 2 | 23 |
| 4 | 67 |
--------------
You can try this
SELECT tbl1.nid,
min(tbl2.cid) as cid
FROM table1 tbl1
INNER JOIN table2 tbl2 ON tbl1.nid=tbl2.nid
GROUP BY tbl2.nid;
SQL Fiddle
Try this
SELECT t1.nid,
min(t2.cid) as cid
FROM table1 t1
INNER JOIN table2 t2 ON t1.nid=t2.nid
GROUP BY t2.nid;
This could also work
select nid, min(cid) cid
from table2
group by nid
The above queries are have issues in group by clause. Kindly check this query.
SELECT t1.NID, MIN(t2.CID) AS cis from
TAB1 t1 inner join TAB2 t2 on t1.nid = t2.nid
group by t1.nid

Counting from another table [mysql]

I have one table like this:
+----+---------+-------------+
| id | site_id | search_term |
+----+---------+-------------+
| 1 | 2 | apple |
| 2 | 2 | banana |
| 3 | 3 | cheese |
| 4 | 1 | aubergine |
+----+---------+-------------+
And another like this:
+----+---------+-------------+
| id | site_id | search_term |
+----+---------+-------------+
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 1 |
| 5 | 3 | 3 |
| 6 | 1 | 4 |
+----+---------+-------------+
I want to find out how many times each search_term shows up in the 2nd table, and how many times.
In other words, from this data, if I was asking about site_id 2, I'd want this to be returned:
+-------------+-------+
| search_term | count |
+-------------+-------+
| apple | 3 |
| banana | 1 |
+-------------+-------+
I'm familiar with basic joins and such, as well as COUNT, but I'm not sure how to count things from another table.
Thanks!
You could join the tables together, and count the number of rows in the second table:
select t1.search_term
, count(t2.id)
from table1 t1
left join table2 t2
on t1.id = t2.search_term
group by t1.search_term
Give this a try:
select t1.search_term, count(*) from t1
join t2 on t1.id = t2.search_term and t1.site_id = t2.site_id
where t1.site_id = 2
group by t1.search_term