Mysql select to table 5 per line - mysql

how i can Select from mysql_db to table limit by 5. 5 per line.
I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
i need:
1 6 11
2 7 12
3 8 13
4 9 14
5 10
or :
1 2 3 4 5
6 7 8 9 10
11 12 13 14
thank you.

Try to adapte this function according to your case :
<?php
$array=array('a','b','c','d','e','f','g');
function display($array,$limite){
$arrays= array_chunk($array, $limite);
foreach($arrays as $array){
echo implode(' ',$array).'<br/>';
}
}
display($array,2);
?>
Output of display($array,2);
a b
c d
e f
g
Output of display($array,3);
a b c
d e f
g

This query will solve your problem. Table named test is joined to himself two times on the interesting condition:
SELECT
t.num as num,
t1.num as num1,
t2.num as num2
FROM
test t
LEFT JOIN test t1 ON(t.num+5 = t1.num)
LEFT JOIN test t2 ON(t1.num+5 = t2.num)
ORDER BY num ASC
LIMIT 5

Related

Mysql Joining four tables

I've been having trouble linking these tables together:
Table 1: Matches
ID
Name
Date
1
Adam vs Lance
2021-09-2021
2
Bex vs Adam vs Erica
2021-08-2021
3
Craig vs Bree
2021-07-2021
4
Danielle vs Alan
2021-06-2021
5
Erica vs Zoe vs AJ
2021-05-2021
6
Bree vs Erica
2021-04-2021
7
Bree vs Lance
2021-03-2021
8
Bree vs Lance vs Zoe
2021-02-2021
Table 2: Winners:
ID
Name
Match ID
IDNum
1
Adam
1
1
2
Bex
2
3
3
Danielle
4
7
4
Zoe
5
9
5
Erica
6
4
6
Bree
7
5
7
Bree
8
5
Table 3: Losers:
ID
Name
Match ID
IDNum
1
Lance
1
2
2
Adam
2
1
3
Erica
2
4
4
Alan
4
8
5
AJ
5
10
6
Erica
5
4
7
Bree
6
5
8
Lance
7
2
9
Lance
8
2
10
Zoe
8
9
Table 3: Draws:
ID
Name
Match ID
IDNum
1
Craig
3
6
2
Bree
3
5
Table 4: Players
ID
Name
Gender
1
Adam
M
2
Lance
M
3
Bex
F
4
Erica
F
5
Bree
F
6
Craig
M
7
Danielle
F
8
Alan
M
9
Zoe
F
10
AJ
F
The query I've been trying is to look up all matches with Bree in them and order them by date.
Table 5: Output:
Match ID
3
6
7
8
Draw: Match ID: 3
Los: Match ID: 6
Win: Match ID: 7
Win: Match ID: 8
When I try to inner join wins & losses against the Match table it works but the second I include the draws it does not return anything.
If I try just returning draws it works but then inner joining either losses or wins causes 0 results.
Can anyone help me with the code that'll work?
Query I'm trying:
SELECT Matches.ID AS MatchID, Winners.Name
FROM Matches
inner JOIN Draws
ON Matches.ID = Draws.MatchID
inner JOIN Winners
ON Matches.ID = Winners.MatchID
inner JOIN Losers
ON Matches.ID = Losers.Match ID
and (Winners.winner_id_num = 5
OR
Losers.type_id_num = 5
OR
Draws.IDNum = 5
)
GROUP BY match_id_num;
I would recommend you to use UNION between the 3 tables with results and join the output with the Matches table.
SELECT r.Result, r.IDMatch FROM (
SELECT *, 'Win' as Result FROM Winners WHERE IDNum = 5
UNION
SELECT *, 'Los' as Result FROM Losers WHERE IDNum = 5
UNION
SELECT *, 'Draw' as Result FROM Draws WHERE IDNum = 5
) AS r
INNER JOIN Matches AS m ON m.ID = r.IDMatch
ORDER BY m.Date DESC
The output would be:
Draw 3
Los 6
Win 7
Win 8

How to calulate SUM previous and current row value in MySQL table?

My table is "Activity_table", which have 4 columns. How can I create a function, which SUM each Peroson 2 previous and current activities?
My Activity_table
ID PID AID Act
1 1 1 12
2 1 2 32
3 2 1 5
4 1 3 21
5 2 2 12
6 2 3 19
7 1 4 11
8 2 4 6
PID-PersonID; AID-ActivitieID; Act-Activitie Value
My target:
ID PID AID Act SUM
1 1 1 12 12
2 1 2 32 44
3 2 1 5 5
4 1 3 21 65
5 2 2 12 17
6 2 3 19 36
7 1 4 11 64
8 2 4 6 37
Sum1=12; Sum2=32+12; Sum3=5; Sum4=21+32+12; Sum5=12+5; Sum6=19+12+5; Sum7=11+21+32; Sum8=6+19+12; Thank you,
To use the two previous and the current row,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND ID >= a.ID - 2
AND PID = a.PID)
FROM Activity_table a;
You are taking the SUM according to the PID right? Then the last two rows of your target should be modified as,
7 1 4 11 **76**
8 2 4 6 **42**
The most flexible query is for your requirement is,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND PID = a.PID)
FROM Activity_table a;
Then if you need only the flow of SUM of a particular PID, you can change it like this,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND PID = a.PID)
FROM Activity_table a
WHERE PID = 2;
Result:
ID PID AID Act SUM
3 2 1 5 5
5 2 2 12 17
6 2 3 19 36
8 2 4 6 42
Sorry for the previous wrong Answers (deleted).
This produces the correct answer based on your example:
SET #LastPID = 0, #Act1 = 0,#Act2 = 0,#Act3 = 0;
SELECT ID,PID,AID,Act,`SUM` FROM
(
SELECT ID,PID,AID,Act,#Act3 := IF(#LastPID != PID, 0,#Act2),#Act2 := IF(#LastPID != PID, 0,#Act1), #Act1 := Act, #Act1 + #Act2 + #Act3 `SUM`, #LastPID := PID
FROM Activity_table
ORDER BY PID,ID
) sm
ORDER BY ID
;

Write correct SQL Query for:- i used query ? Data is coming from multiple tables

I used query:-
select
employees.N, employees.ID, Date(Month) as m, count(*) as P,
(FullOrHalfLeave) as FOH
from
employees, leaveapplication, leaveapplicationdetails
where
employees.ID = leaveapplication.ID
and leaveapplication.LeaveApplicationID = leaveapplicationdetails.Leave_Application_ID
and TypeOfLeave = 1
and LeaveStatus = 4
and employees.ID = 18
group by
employees.ID,FOH
order by
employee.N, FOH
I get a result like below:-
N ID m P FOH
--------------------------------------
A 18 1 8 1
A 18 1 3 2
A 18 4 6 1
A 18 5 3 1
A 18 5 1 2
A 18 6 3 2
A 18 9 1 1
A 18 9 4 2
I want result like this:
N ID m Pad FOH
----------------------------------------
A 18 1 9.5 -
A 18 4 6 -
A 18 5 3.5 -
A 18 6 1.5 -
A 18 9 3 -
You don't need FOH returned, just do:
select employees.N, employees.ID, Date(Month) as m, SUM(1 / FullOrHalfLeave) as P
this way, if FOH = 1 then you'll be counting one day and if FOH = 2 you'll be counting half day.

Min Max Value in coloumn but with a certain condition

I have a MySql table with 3 coluomn : Nip, Bidang and Total.
I want to count min and max value of total with the same bidang. but i don't want to count min and max value for all coloumns.
Sample data:
NIP Bidang Total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
And the result column:
MIN
1
1
1
4
4
8
8
Simply do this:
SELECT * FROM
(
(SELECT Bidang FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(Total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
BIDANG MINVAL MAXVAL
A 1 10
A 1 10
A 1 10
B 4 7
B 4 7
C 8 9
C 8 9
See result in SQL Fiddle.
EDIT
To see the Total column also, add Total to first query.
SELECT * FROM
(
(SELECT NIP,Bidang,Total FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
NIP BIDANG TOTAL MINVAL MAXVAL
1 A 10 1 10
2 A 5 1 10
3 A 1 1 10
4 B 4 4 7
5 B 7 4 7
6 C 8 8 9
7 C 9 8 9
See result in SQL Fiddle.
Select bidang, min(total) MyMin, Max(total) myMax
From tableName
group by bidang
NIP bidang total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
should return
A 1 10
B 4 7
C 8 9

update statement with aggregate function

Good day everyone..!:-)
I have this table tab where totalUsed is equal to the sum of all used values referenced to name
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 0
7 a 5 0
i have this code but it only copy totalUsed's adjacent used value
UPDATE tab
SET totalUsed=
(
SELECT SUM(used)
)
cid name used total
1 a 1 1
2 a 3 3
3 a 6 6
4 b 3 3
5 b 7 7
6 b 10 10
7 a 5 5
if used is set as 10 for cid 6, totalUsed should be 20
and for cid 7 it should be 15.
how to do it in mysql?
it should look like this.
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 20
7 a 5 15
thanks for help
:-)
In most dialects of SQL, you can do this using an update with a correlated subquery:
UPDATE tab
SET totalUsed = (SELECT SUM(used)
from tab tab2
where tab2.name = tab.name and
tab2.cid <= tab.cid
);
EDIT:
The above will not work in MySQL. You can do this instead:
UPDATE tab join
(select tab2.cid,
(SELECT SUM(used)
from tab tab3
where tab3.name = tab2.name and
tab3.cid <= tab2.cid
) as cum_used
from tab tab2
) tab2
on tab.cid = tab2.cid
SET tab.totalUsed = tab2.cum_used;