except alternative on mysql - mysql

I need to have first select values that are not in the second select.
select tnum,user from resp order by tnum, user
except
select test.tnum,cursa.user from cursa inner join test on test.curso = cursa.curso;
results:
select tnum,user from resp order by tnum, user;=
tnum user
1 1
1 7
1 8
1 10
2 7
select test.tnum,cursa.user from cursa inner join test on test.curso = cursa.curso;=
tnum user
1 1
1 7
1 8
1 10
2 1
2 8
3 1
3 7
3 8
3 10
4 1
4 7
4 8
4 10
I need of return tnum 2 and user 7.

This would often be solved using not exists:
select r.tnum, r.user
from resp r
where not exists (select 1
from cursa c inner join
test t
on t.curso = c.curso
where t.tnum = r.tnum and c.user = r.user
);
This has a slight difference in how it handles NULL values. If either rep.tnum or resp.user are NULL, then the row will not be removed.
If this is a possibility, then change the where clause in the subquery to:
where (t.tnum = r.tnum or (t.tnum is null and r.tnum is null) ) and
(c.user = r.user or (c.user is null and r.user is null) )

You can use a LEFT JOIN:
select tnum, user
from resp AS t1
left join (
select test.tnum, cursa.user
from cursa
inner join test
on test.curso = cursa.curso ) AS t2
ON t1.tnum = t2.tnum AND t1.user = t2.user
WHERE t2.num IS NULL AND t2.user IS NULL
order by tnum, user
The WHERE clause filters out all rows of resp that are related to rows of the derived table.

Related

MySQL Select first entry of GROUP after custom ORDER BY

I'm trying to fetch the first entry of each group after the custom ORDER BY but don't know how to select that first entry of each group. The groups should be ordered by the #team if exist, otherwise other team else NULL.
SELECT t1.*
FROM tbl t1
INNER JOIN tbl2 t2 ON t2.group_id = t1.group
WHERE t2.region = #region
ORDER BY
CASE
WHEN team=#team THEN 1
WHEN team is NOT NULL THEN 2
WHEN team is NULL THEN 3
END
Content of tbl
id group team
1 1 AA
2 1 BB
3 2 AA
4 2 CC
5 3 BB
6 3 NULL
7 4 NULL
Expected result when #team=AA
id group team
1 1 AA
3 2 AA
5 3 BB
7 4 NULL
Expected result when #team=BB
id group team
2 1 BB
3 2 AA
5 3 BB
7 4 NULL
Use your custom ordering logic with ROW_NUMBER:
WITH cte AS (
SELECT t1.*, ROW_NUMBER() OVER (PARTITION BY t1.`group`
ORDER BY CASE WHEN team = #team THEN 1
WHEN team IS NOT NULL THEN 2
ELSE 3 END) rn
FROM tbl t1
INNER JOIN tbl2 t2 ON t2.group_id = t1.`group`
WHERE t2.region = #region
)
SELECT id, `group`, team
FROM cte
WHERE rn = 1;
Side note: Avoid naming your table columns GROUP, as this is a reserved MySQL keyword, and therefore must always be escaped in backticks.

Filter a SQL table according to a condition

I have a table like this in MySQL :
Group Seqno Event
1 1 A
1 2 B
1 3 C
1 4 B
1 5 E
1 6 B
1 7 D
1 8 A
I want to count all the rows from last (most recent entry) for each Group with Event = B, and return all remaining rows as soon as it hit count of 2.
The output will be
Group Seqno Event
1 4 B
1 5 E
1 6 B
1 7 D
1 8 A
Any idea how to achieve it.
You seem to want all rows from the second to last "B"?
If so, you can use a correlated subquery:
select t.*
from t
where t.seqno >= (select t2.seqno
from t t2
where t2.group = t.group and t2.event = 'B'
order by t2.seqnum desc
limit 1, 1
);
To handle the case where there may be no "second" sequence number, you can use coalesce():
select t.*
from t
where t.seqno >= coalesce( (select t2.seqno
from t t2
where t2.group = t.group and t2.event = 'B'
order by t2.seqnum desc
limit 1, 1
), t.seqno
);

Mysql Multiplying and adding Result of Subquery

Hi I am new to my sql I am trying to multiply and add the result of my subquery however my query is not working it gives me 0 output always for each rows
my column is like this Output of total column is wrong
Val1 Income Val2 Total
1 5 5 0
1 1 8 0
1 1 7 0
5 7 6 0
my query is like this
SELECT
COUNT(DISTINCT t1.id) AS 'Val1',
(SELECT SUM(CAST(COALESCE(r.t_payment_total,0) AS DECIMAL(18,2))) AS 'Income'
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status!="Pending" && r.t_status!="Booked" AND r.c_mid = m.id AND t.t_type_id = t1.t_type_id
)AS 'Income',
num1 AS 'Val2',
'Val1'*'Income'+ 'Val2' as 'Total'
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id AND mp.t_type = 'try' AND mp.t_system_id = 'ok'
GROUP BY t1.t_tool_type_id
ORDER BY m.t_m ASC, tt.t_ttype ASC, mf.t_floor ASC;
Desired Output is like this. I will really appreciate any help or advice Thank you
Val1 Income Val2 Total
1 5 5 10
1 1 8 8
1 1 7 7
5 7 6 41

how is this query done in mysql?

Given the following table ...
ID USER NUM
1 1 69
2 2 75
3 3 7
4 1 31
5 2 18
6 3 70
7 1 12
8 2 23
9 3 42
... which query would return rows with the lowest NUM for each USER?
The result should look like ...
ID USER NUM
7 1 12
5 2 18
3 3 7
Can't wrap my head around this one! Assuming it has a GROUP BY, but everything I try fails... Any pointers?
SELECT t.*
FROM tablename t
INNER JOIN (SELECT user, MIN(num) num
FROM tablename
GROUP BY user) x ON t.user = x.user AND t.num = x.num
or
SELECT t1.*
FROM tablename t1
LEFT JOIN tablename t2 ON t1.user = t2.user AND t1.num > t2.num
WHERE t2.id IS NULL
SELECT ID , MIN(NUM) as MIN_NUM , USER FROM usertable GROUP BY USER
Have a look at demo http://sqlfiddle.com/#!2/ce2fd/1
HEre is another method. UPDATED WITH THE CORRECT REREFERENCE
SQLFIDDLE
Query
select b.id, a.user, a.minnum from
mytable as b
join
(select user, min(num) as minnum
from mytable
group by user
) as a
on b.user = a.user
where b.num = a.minnum
order by a.minnum asc
limit 3
;
Results:
ID USER MINNUM
3 3 7
7 1 12
5 2 18

How to update by reading and looping through all the rows?

The following is an example structure:
table `pligg`
#id #alpha #num
1 a null
2 b null
3 c null
4 a null
5 d null
6 b null
7 a null
8 e null
I'd like to update the databse like this after a single mysqli query:
table `pligg`
#id #alpha #num
1 a 1
2 b 1
3 c 1
4 a 2
5 d 1
6 b 2
7 a 3
8 e 1
What I'm trying to do is to update the column num with the number of duplicate.
I tried this query, but in vain
UPDATE pligg SET 'num' = COUNT(DISTINCT alpha) WHERE 'id'<id
This should do the trick:
UPDATE pligg a
INNER JOIN
(
SELECT a.id, a.alpha, COUNT(1) AS dup_cnt
FROM pligg a
INNER JOIN pligg b ON a.id >= b.id AND a.alpha = b.alpha
GROUP BY a.id, a.alpha
) b ON a.id = b.id
SET a.num = b.dup_cnt
SQLFiddle Demo