about SQL Row ascending order - php

I have this 2 table in database in sql
TABLE1:
USER id | state| TYPE | time |
1 | 1 | 1 | time |
2 | 1 | 2 | time |
3 | 1 | 2 | time |
4 | 1 | 2 | time |
5 | 0 | 1 | time |
6 | 0 | 1 | time |
TABLE2:
id |USER id| run
1 | 3 | 7
2 | 1 | 5
3 | 1 | 5
4 | 4 | 8
5 | 2 | 6
6 | 2 | 6
7 | 3 | 7
8 | 3 | 7
9 | 3 | 7
10 | 3 | 7
11 | 2 | 6
12 | 4 | 8
13 | 4 | 8
14 | 1 | 5
15 | 2 | 6
16 | 2 | 6
17 | 5 | 9
18 | 4 | 8
I am printing this
SELECT * FROM TABLE1 WHERE state != 0
it will print row this way
USER ID 1
USER ID 2
USER ID 3
USER ID 4
But I want to ascending this by count
WHERE, Count = num of row of TABLE2
Where user id=1 or 2 or..N
Here:
count of USER id 1 = 3
count of USER id 2 = 5
count of USER id 3 = 5
count of USER id 4 = 4
Now i want to ascending
Table 1 where high count to low count from table 2 and high run to low run
USER ID 3
USER ID 2
USER ID 4
USER ID 1
Using PHP AND MYSQLI
Please help me

select t1.id, count(t2.id) as t2_count
from table1 t1
left join table2 t2 on t1.`user id` = t2.`user id`
group by t1.id
order by t2_count desc

Related

Store data into three dimensional array PHP

I have a set of data stored in database table tb_Alt as follows :
id_alt |name|distance|price|quantity
1 |A | 2 | 10 | 3
2 |B | 4 | 123 | 4
3 |C | 1 | 201 | 1
4 |D | 5 | 145 | 10
And tb_Cri as follows :
id_criteria |name | weight |
1 |distance | 10
2 |price | 20
3 |quantity | 30
Now i want to insert data dynamically from tb_A into tb_B with structure as follows :
# |id_alt | id_criteria | value
1 | 1 | 1 | 2
2 | 1 | 2 | 10
3 | 1 | 3 | 3
4 | 2 | 1 | 4
5 | 2 | 2 | 123
6 | 2 | 3 | 4
7 | 3 | 1 | 1
8 | 3 | 2 | 201
9 | 3 | 3 | 1
10| 4 | 1 | 5
11| 4 | 2 | 145
12| 4 | 3 | 10
How can i achieve this through PHP?
Here's what i tried so far :
$data = $obj->selTable("SELECT * FROM tbb_Alt");
//$finishArr = transposeArr($data);
$totalLoop = count($data)*3;
$interval = (int)$totalLoop/3;
$counter = $totalLoop+$interval;
$j = 0;
$k = 0;
for($i=0;$i<=$counter;$i++){
if($i%4 !== 0){
if($k%4 == 0) $k=1;
// code to insert data into tb_B
// the part where i got stuck
echo $j.' '.$k++.' ';
continue;
}
$j++;
}
INSERT INTO tab_b (name,value)
SELECT name,value FROM (
SELECT id, 1 as id2, name,distance as value FROM tb_A
UNION SELECT id, 2 as id2, name,price as value FROM tb_A
UNION SELECT id, 3 as id3, name,quantity as value FROM tb_A
ORDER BY id, id2
) t;
It's always best to use in database calculations live example # SQL Fiddle

copy (with update) certain content from the same table

I wanted to UPDATE the value of my below table (row & col_md) :
Current Data
| id | id_cat | row | col_md |
| --- | ------ | ---- | ------ |
| 1 | 1 | 1 | 4 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 3 | 5 |
| 4 | 2 | 1 | 3 |
| 5 | 2 | 2 | 4 |
| 6 | 2 | 2 | 4 |
| 7 | 3 | 1 | 12 |
| 8 | 3 | 1 | 12 |
| 9 | 3 | 2 | 3 |
That may look something like the below table. (I want to have the same content of rows that id_cat=1 have, in rows with id_cat=2 & 3).
Required Data:
| id | id_cat | row | col_md |
| --- | ------ | ---- | ------ |
| 1 | 1 | 1 | 4 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 3 | 5 |
| 4 | 2 | 1 | 4 |
| 5 | 2 | 2 | 5 |
| 6 | 2 | 3 | 5 |
| 7 | 3 | 1 | 4 |
| 8 | 3 | 2 | 5 |
| 9 | 3 | 3 | 5 |
id_cat 2 and 3 should have the same "row" and "col_md" values as in id_cat=1.
I've tried with this post first answer like this:
UPDATE `myTable` AS t1 JOIN `myTable` AS t2 ON t2.id_cat=1
SET t1.row = t2.row, t1.col_md = t2.col_md
WHERE t1.id_cat = 2 or t1.id_cat=3;
but that results on all "row" column values equal to 1.
What I'm doing wrong and what's the way to do this right?
EDIT:
The tables above are just examples to make this ask easier to understand, but the real table is bigger (4k rows) and:
"row" column with id_cat=1 can have any number and not a sequence as in the example.
"col_md" columns can have any number too.
That's why the update must set a copy of the id_cat=1 "row" and "col_md" values in the id_cat!=1 "row" and "col_md" values.
If this can't be done with just MySQL, a php script will be nice too.
In the example query you gave, you are updating t1.row with t2.row. As you are joining on the id_cat, this will result in multiple rows selected to update a single row, so the outcome just takes the first row.
What you actually want, is to make the 1-to-1 relation in the update, so what needs to be changed in your query is to add the row matching in the join and remove the assignment in the SET, like this:
UPDATE `myTable` AS t1 JOIN `myTable` AS t2 ON t2.id_cat=1 AND t1.row = t2.row
SET t1.col_md = t2.col_md
WHERE t1.id_cat = 2 or t1.id_cat=3;
Which then gives the output of:
MariaDB [testart]> select * from myTable;
+------+--------+------+--------+
| id | id_cat | row | col_md |
+------+--------+------+--------+
| 1 | 1 | 1 | 4 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 3 | 5 |
| 4 | 2 | 1 | 4 |
| 5 | 2 | 2 | 5 |
| 6 | 2 | 3 | 5 |
| 7 | 3 | 1 | 4 |
| 8 | 3 | 1 | 4 |
| 9 | 3 | 2 | 5 |
+------+--------+------+--------+
9 rows in set (0.00 sec)
Currently able to achieve the SQL query for your desired result.
SELECT t2.id_cat, t1.row, t1.col_md
FROM (SELECT row, col_md from mytable WHERE id_cat=1) as t1 , mytable as t2
GROUP BY t2.id_cat, t1.row, t1.col_md
The above will return the following..
I suggest to use INSERT statement along with the above query to put the record into a new table and drop the old one.
Cheers!
EDITED...
Instead of Updating table, alternate approach could be to Insert the required record into a new table.
This can be achieved with following four steps
Create a tmp table with same fileds (id Auto_Increment, id_cat, row, col_md)
Insert to tmp table with this statement...
INSERT INTO tmp(id_cat, row, col_md)
SELECT t2.id_cat, t1.row, t1.col_md
FROM (SELECT row, col_md from mytable WHERE id_cat=1) as t1 , mytable as t2
GROUP BY t2.id_cat, t1.row, t1.col_md
Remove/Rename 'myTable'.
Rename 'tmp' table to 'myTable'.
Hope this will serve the purpose...
Cheers!
it's not enough to tell which group you want the data from, you need to match id to id.
in your case t2.id 4 and 7 to t1.id 1, t2.id 5 and 8 to t1.id 2, and t2.id 6 and 9 to t1.id 3.
SELECT #d := COUNT(*) FROM myTable WHERE id_cat = 1;
UPDATE `myTable` AS t1
JOIN `myTable` AS t2 ON t2.id_cat=1 AND
t2.id = IFNULL(NULLIF(t1.id MOD #d, 0), #d)
SET t1.row = t2.row, t1.col_md = t2.col_md
WHERE t1.id_cat = 2 or t1.id_cat=3;
#d holds the number of lines where id_cat = 1
we divide t1.id by #d and match the remainder (MOD) to t2.id.
when t1.id is multiple of #d the remainder is 0 and we have to match it to #d
so we make 0 into NULL and NULL into #d
In my understanding, the difficult part about this question is to relate each record to update (ie each record with id_cat IN (2, 3)) to the relevant original record (record with id_cat = 1).
Based on your sample data, I understand that you expect series of records for each id_cat (I can see three groups of three records, sorted by increasing id), so I would assume that you want to relate each record to the original that has the same sequence in the group of record where id_cat = 1.
Assuming MySQL 8.0, a typical approach to assign a number to a record within a group is ROW_NUMBER(). Consider this simple query:
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY id_cat ORDER BY id) rn
FROM t
Yields:
| id | id_cat | rw | col_md | rn |
| --- | ------ | --- | ------ | --- |
| 1 | 1 | 1 | 4 | 1 |
| 2 | 1 | 2 | 5 | 2 |
| 3 | 1 | 3 | 5 | 3 |
| 4 | 2 | 1 | 3 | 1 |
| 5 | 2 | 2 | 4 | 2 |
| 6 | 2 | 2 | 4 | 3 |
| 7 | 3 | 1 | 12 | 1 |
| 8 | 3 | 1 | 12 | 2 |
| 9 | 3 | 2 | 3 | 3 |
Now with this set-up in mind, we can turn this query to a Common Table Expression (available also starting MySQL 8.0), and JOIN it as need with the original table to do the UPDATE:
WITH cte AS (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY id_cat ORDER BY id) rn
FROM t
)
UPDATE t t0
INNER JOIN cte t1 ON t1.id = t0.id
INNER JOIN cte t2 ON t2.id_cat = 1 AND t2.rn = t1.rn
SET t0.rw = t2.rw, t0.col_md = t2.col_md
WHERE t0.id_cat IN (2, 3)
Details:
t0 is the original table, where records having id_cat IN (2, 3) need to be updated
t1 is the corresponding record in the CTE (to which a row number was assigned)
t2 is the record in the CTE that has id_cat = 1 and the same row number as the record being updated
Demo on DB Fiddle:
| id | id_cat | rw | col_md |
| --- | ------ | --- | ------ |
| 1 | 1 | 1 | 4 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 3 | 5 |
| 4 | 2 | 1 | 4 |
| 5 | 2 | 2 | 5 |
| 6 | 2 | 3 | 5 |
| 7 | 3 | 1 | 4 |
| 8 | 3 | 2 | 5 |
| 9 | 3 | 3 | 5 |

how to fetch this query using 2 table

Hy I have 2 table
1.application
id | name | status
====================
1 | morvick | complete
2 | siti | prosess
3 | boby | complete`
2.application_test
id | application_id | test_id | result
======================================
1 | 1 | 1 | 70
2 | 1 | 2 | 80
3 | 1 | 3 | 90
4 | 2 | 1 | 60
5 | 2 | 2 | 80
6 | 2 | 3 | 70
7 | 3 | 1 | 90
8 | 3 | 2 | 70
9 | 3 | 3 | 60
10| 3 | 4 | 80
my Question is :
==================
1. how to find the maximum value at each test_id
2. how I can to get or total applicant_id where status complete
for example to be like this :
test_id | result_max | total_applicant_status(complete)
1 | 90 | 2
2 | 80 | 2
3 | 90 | 2
4 | 80 | 1
SELECT MAX(value) FROM table WHERE test_id = 1;
or perhaps SELECT value, test_id FROM table ORDER BY value DESC;
and for the next part, this may give what you want.
SELECT at.test_id, MAX(at.result), COUNT(IF(status='complete', 1, 0)) FROM application a LEFT JOIN application_test at ON a.id = at.application_id GROUP BY application_id;

How to write this complex SQL statement

I have three identical tables in my MySQL table namely
first_term_result, second_term_result and third_term_result
this are the columns in it
exam_type_id | student_id | subject_id | mark |
or example with dummy data
NOTE: there are three different exam type for each subjects (CA1, CA2, CA3 and Exam),
there are three table like this with same thing but different data as it hold data for first term another for second term and the last for third term.
first_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 12 |
2 | 6 | 7 | 9 |
3 | 6 | 7 | 13 |
4 | 6 | 7 | 45 |
1 | 4 | 7 | 7 |
2 | 4 | 7 | 5 |
3 | 4 | 7 | 10 |
4 | 4 | 7 | 34 |
second_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 15 |
2 | 6 | 7 | 6 |
3 | 6 | 7 | 10 |
4 | 6 | 7 | 50 |
1 | 4 | 7 | 6 |
2 | 4 | 7 | 3 |
3 | 4 | 7 | 9 |
4 | 4 | 7 | 44 |
third_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 17 |
2 | 6 | 7 | 8 |
3 | 6 | 7 | 15 |
4 | 6 | 7 | 67 |
1 | 4 | 7 | 12 |
2 | 4 | 7 | 8 |
3 | 4 | 7 | 12 |
4 | 4 | 7 | 50 |
Now what i want to achieve is get the SUM() of first_term_result.mark second_term_result.mark and third_term_result.mark of each students WHERE subject_id=7 group by students name.
another very important problem is i will be calculating the grand sum for each students for first_term+second_term+third_term and also want to be able to order the grand total for that student and the subjects in DESC so i can position them accordingly please if it will be easier on php please let me know.
Thanks
it seems very complex to me but i know there are gurus here who ca achieve this, i read somewhere that it is possible to order by even when rollup is used.
below is my code which doesn't work obviously.
SELECT CONCAT(s.fname,' ',s.mname,' ',s.lname) AS sname,
SUM(f.mark) AS first_total,
SUM(se.mark) AS second_total,
SUM(t.mark) AS third_total
SUM(f.first_total,second.total,third_total) as GT // just to show my intention
FROM students s, first_term_result f, second_term_result se, third_term_result t
WHERE s.studentID=f.student_id AND
s.studentID=se.student_id AND
s.studentID=t.student_id AND
f.subject_id=7 AND
se.subject_id=7 AND
t.subject_id=7
GROUP BY sname ORDER BY GT DESC
SELECT CONCAT(MS.fname,' ',MS.mname,' ',MS.lname) AS sname,
M.FTotal, M.STotal, M.TTotal,
(M.FTotal + M.STotal + M.TTotal) AS GrandTotal
FROM (
SELECT st.studentID,
(
SELECT SUM(f.mark)
FROM first_term_result AS f
WHERE f.subject_id = 7
AND f.student_id = st.studentID
) AS FTotal,
(
SELECT SUM(s.mark)
FROM second_term_result AS s
WHERE s.subject_id = 7
AND s.student_id = st.studentID
) AS STotal,
(
SELECT SUM(t.mark)
FROM third_term_result AS t
WHERE t.subject_id = 7
AND t.student_id = st.studentID
) AS TTotal
FROM students AS st
WHERE st.studentID IN (
SELECT studentID
FROM first_term_result AS fs
WHERE fs.subject_id = 7
AND fs.student_id = st.studentID)
GROUP BY st.studentID
) AS M
JOIN students MS ON M.studentID = MS.studentID
ORDER BY (M.FTotal + M.STotal + M.TTotal) DESC

Custom MySQL Ordering

I have a query that needs a custom sorting, trimmed down to the bare minimums something like:
SELECT u.*, p.*, p.id as product_id
FROM users u, products p
WHERE u.id = p.user_id
ORDER BY product_id DESC
And I get returned a set of rows like:
UserID ProductID
2 5
2 4
3 3
1 2
1 1
But I want it to actually sort SOMETHING like this (so no 2 UserIDs are adjacent to eachother):
UserID ProductID
1 2
2 4
3 3
2 5
1 1
Is this even possible with MySQL, or do I need some PHP magic?
A canonical way of solving this problem is by enumerating the duplicate rows and then ordering by that value:
select t.*
from (SELECT u.*, p.*, p.id as product_id,
row_number() over (partition by u.id order by (select NULL)) as seqnum
FROM users u join
products p
on u.id = p.user_id
) t
order by seqnum, id;
This will work, as long as no one user has a really long sequence (as in your example).
There is no "always-works" solution, because it is easy to come up with a situation where your goal is not possible.
Here fetch your sorted results into an array. Then do something like this.
$records = $res->fetchAll();
$count = count($records);
$records = array_chunk($records, ceil(count($records)/2);
$unsorted = array();
for($x = 0; $x < $count; $x++){
$unsorted[] = $records[$x%2][floor($x/2)];
}
Consider the following...
CREATE TABLE sortable(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,player_id INT NOT NULL);
INSERT INTO sortable(player_id) VALUES (1),(1),(2),(3),(4),(3),(3),(2),(1),(2),(4),(4);
SELECT * FROM sortable;
+----+-----------+
| id | player_id |
+----+-----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 3 |
| 7 | 3 |
| 8 | 2 |
| 9 | 1 |
| 10 | 2 |
| 11 | 4 |
| 12 | 4 |
+----+-----------+
SELECT x.*,COUNT(*) rank FROM sortable x JOIn sortable y ON y.player_id = x.player_id AND y.id <= x.id GROUP BY x.id ORDER BY player_id,rank;
+----+-----------+------+
| id | player_id | rank |
+----+-----------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 9 | 1 | 3 |
| 3 | 2 | 1 |
| 8 | 2 | 2 |
| 10 | 2 | 3 |
| 4 | 3 | 1 |
| 6 | 3 | 2 |
| 7 | 3 | 3 |
| 5 | 4 | 1 |
| 11 | 4 | 2 |
| 12 | 4 | 3 |
+----+-----------+------+
SELECT x.*,COUNT(*) rank FROM sortable x JOIn sortable y ON y.player_id = x.player_id AND y.id <= x.id GROUP BY x.id ORDER BY rank;
+----+-----------+------+
| id | player_id | rank |
+----+-----------+------+
| 1 | 1 | 1 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 4 | 1 |
| 2 | 1 | 2 |
| 8 | 2 | 2 |
| 6 | 3 | 2 |
| 11 | 4 | 2 |
| 9 | 1 | 3 |
| 10 | 2 | 3 |
| 7 | 3 | 3 |
| 12 | 4 | 3 |
+----+-----------+------+
So if your problem is just that you dont want two records with same id should not come next to each other wha I can think simplest is use
SELECT u.*, p.*, p.id as product_id
FROM users u, products p
WHERE u.id = p.user_id
ORDER BY user_id%2 DESC
Or you can even use other number than 2 to meet any certain order you want....

Categories