Check for mysql ascending field - php

I have a field in my mysql database called 'rank'.
I want to run a simple as possible query or php loop that checks if there's an increment starting from 1 in this field across all rows. Doesn't matter the order of the rows, just as long as there's a row with 'rank' of 1, another of 2, another of 3, etc.
I have some code to run if there's a break in the sequence so Im just looking for help on the most resource friendly way of checking.
There needs to be a row with a rank of 1 also.

This will return a row with 0 as the missingrank if rank 1 exists, and any rows in addition to missingrank 0 are missing ranks.
SELECT T.ID - 1 AS [MISSINGRANK]
FROM URTABLE T
LEFT JOIN URTABLE T2 ON T.rank = T2.rank + 1
WHERE T2.ID IS NULL

take a look to this:
mysql> set #a:=1;
mysql> select actor_id,if(#a<>actor_id,'break',#a),#a:=#a+1 from t2 order by actor_id;
+----------+-----------------------------+----------+
| actor_id | if(#a<>actor_id,'break',#a) | #a:=#a+1 |
+----------+-----------------------------+----------+
| 1 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 3 | 4 |
| 4 | 4 | 5 |
| 5 | 5 | 6 |
| 6 | 6 | 7 |
| 7 | 7 | 8 |
.....
| 99 | 99 | 100 |
| 101 | break | 101 |
| 102 | break | 102 |
| 103 | break | 103 |
| 104 | break | 104 |
| 105 | break | 105 |
+----------+-----------------------------+----------+
207 rows in set (0.00 sec)
I don't know if it's what you want to (I tried in local host), where breaks mean that the actor_id has broken the auto_auto_increment, you can add group by , order by, etc, whatever you need

Related

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 |

MySQL group rows by one column sorting by another column

I'm developing a PHP script, and I have the following table:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 1 | 1 | 2 | -B-A-C- |
| 2 | 1 | 3 | -B-C- |
| 3 | 2 | 4 | -C-B- |
| 4 | 3 | 1 | -A-B- |
| 5 | 1 | 6 | -A-C- |
| 6 | 2 | 5 | -C-B- |
| 7 | 2 | 7 | -B-C- |
| 8 | 3 | 8 | -A- |
+----+-----------+----------+--------------+
From this table I would like to select all the rows with "feature" LIKE "%-A-%", but displaying first the result with lowest "position", then all the rows that have same value for column "id_parent" of the first result, then row with the 2nd lowest "position" and all the rows that have same "id_parent" of the result with the 2nd lowest "position", and so on...
So the final result should be:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 4 | 3 | 1 | -A-B- |
| 8 | 3 | 8 | -A- |
| 1 | 1 | 2 | -B-A-C- |
| 5 | 1 | 6 | -A-C- |
+----+-----------+----------+--------------+
For some reason I can't explain here I need to have and HAVING clause for selecting the right "feature" value (...HAVING 'feature' LIKE '%-A-%' ...).
Is it possible to make all this with MySQL (possibly without subqueries) or by processing data results with PHP?
Does this help? I've left the last part of the problem as an exercise for the reader...
SELECT a.*
, c.*
FROM my_table a
JOIN
( SELECT id_parent, MIN(position) position FROM my_table WHERE feature = 'a' GROUP BY id_parent ) b
ON b.id_parent = a.id_parent
AND b.position = a.position
JOIN my_table c
ON c.feature = a.feature
AND c.id_parent = a.id_parent;

PHP MySQL Ranking will count order by number and sort by group

Let's say initially I has table like this without rank or already has rank if php code has running before
ID | STATUS | GRADE | RANK
1 | FAIL | 99 |
2 | FAIL | 95 |
3 | PASS | 40 |
4 | BAR | 99 |
5 | PASS | 70 |
6 | PASS | 85 |
7 | BAR | 80 |
8 | FAIL | 60 |
9 | BAR | 50 |
The ranking system should choose PASS > FAIL > BAR in order. But in that category, when start to enter new category the ranking system will count from previous.
Expected result:
ID | STATUS | GRADE | RANK
1 | FAIL | 99 | 4
2 | FAIL | 95 | 5
3 | PASS | 40 | 3
4 | BAR | 99 |
5 | PASS | 70 | 2
6 | PASS | 85 | 1
7 | BAR | 80 |
8 | FAIL | 60 | 6
9 | BAR | 50 |
Also if can show me some technique to edit the code so that in another day if i plan to upgrade the coding to
ID | STATUS | GRADE | RANK
1 | FAIL | 99 | 4
2 | FAIL | 95 | 5
3 | PASS | 40 | 3
4 | BAR | 99 | 7
5 | PASS | 70 | 2
6 | PASS | 85 | 1
7 | BAR | 80 | 8
8 | FAIL | 60 | 6
9 | BAR | 50 | 9
I have try edit and learning some codes that I found here but I not familiar with session variable. This code will use in cron job/day
You can use user variables to set a rank, and using a sub query joined against the table you want to update you can update the rank in the table.
Something like this:-
UPDATE sometable
INNER JOIN
(
SELECT id,
`status`,
grade,
#rank:=#rank + 1 AS rank
FROM
(
SELECT id,
`status`,
grade
FROM sometable
ORDER BY FIELD(`status`, 'PASS', 'FAIL', 'BAR'), Grade DESC
) sub0
CROSS JOIN (SELECT #rank:=0) sub1
) sub2
ON sometable.id = sub2.id
SET sometable.rank = sub2.rank
Note that I have used the FIELD function to order by the status. In this case it is not strictly necessary (as you could order by status DESC) but given the names of the status would not normally be expect to be in a meaningful alphabetic order FIELD allows you to order in a specified order.

How to implode the value of a record to multiple rows using MySQL?

I have a table like this:
// mytable
+----+--------+-------------------+
| id | word | numbers |
+----+--------+-------------------+
| 1 | hello | 1<br>2<br>3<br> |
| 2 | how | 12<br>15<br> |
| 3 | are | 453<br>1<br> |
| 4 | you | 3<br>33<br>453<br>|
+----+--------+-------------------+
And I want this output:
// mynewtable
+----+--------+---------+
| id | word | numbers |
+----+--------+---------+
| 1 | hello | 1 |
| 2 | hello | 2 |
| 3 | hello | 3 |
| 4 | how | 12 |
| 5 | how | 15 |
| 6 | are | 453 |
| 7 | are | 1 |
| 8 | you | 3 |
| 9 | you | 33 |
| 10 | you | 453 |
+----+--------+---------+
I can do that using PHP. First I have to fetch all rows and implode them by <br> then insert them again. But now I want to know, is there any better solution? (specially using pure MySQL)
Try this solution through query
SELECT #row := #row + 1 as row, t.word, t.number FROM (
SELECT
word,
SUBSTRING_INDEX(SUBSTRING_INDEX(numbers, '<br>', n.digit+1), '<br>', -1) number
FROM split_rec
INNER JOIN
(SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) n
ON LENGTH(REPLACE(numbers, '<br>' , '')) <= LENGTH(numbers)-n.digit
ORDER BY id, n.digit
) AS t, (SELECT #row := 0) r
WHERE t.number <> ''
Output
row word number
1 hello 1
2 hello 2
3 hello 3
4 how 12
5 how 15
6 are 453
7 are 1
8 you 3
9 you 33
10 you 453
While I think that it might be possible using stored procedures, MySQL variables and a liberal use of LIKE "%...%"statements, it would be an order of magnitude more complex than a PHP solution.
Furthermore, I think it is not possible at all if you don't have access to stored procedures as you would need dynamically create new INSERT statements based on what you find in that last column.
TL;DR: No, make yourself happy and use PHP.

MySQL MIN() limit by column

I have a MySQL table that is formatted as follows:
group_clue:
---------------------------------------------------
| id | group_id | clue_id | completed | run_order |
---------------------------------------------------
| 1 | 1 | 2 | 1 | 1 |
| 2 | 1 | 6 | 1 | 2 |
| 3 | 1 | 4 | 1 | 3 |
| 4 | 1 | 3 | 0 | 4 |
| 5 | 1 | 1 | 0 | 5 |
| 6 | 1 | 5 | 0 | 6 |
| 7 | 2 | 9 | 1 | 1 |
| 8 | 2 | 2 | 0 | 2 |
...
---------------------------------------------------
The data above in the group_clue is constructed such that each group_id has every clue_id at some run_order (ranging from 1 to the number of clue_ids and not repeating for a particular group).
First Question
I want to create a table showing the first clue_id for each group_id where completed = 0 when ordered by run_order (aliased as current_clue). Using the above example, this would give:
---------------------------
| group_id | current_clue |
---------------------------
| 1 | 3 |
| 2 | 2 |
---------------------------
My preliminary attempt is:
SELECT group_id, MIN(clue_id) as current_clue
FROM group_clue
WHERE completed = 0
GROUP BY group_id
However, this returns the same clue_id for each group_id.
Second Question
From the data in the first question, I would like to compose a final table where I GROUP_CONCAT() these results so that it contains every current_clue and each group_id that contains that current_clue. I would also like it ordered from those clues with the most group_ids to those with the fewest. An example resulting table is:
--------------------
| clue | group_ids |
--------------------
| 3 | 1,5,4,3 |
| 2 | 2,6 |
--------------------
I cannot figure out the ordering. My preliminary attempt is:
SELECT clue_id, GROUP_CONCAT(group_id)
FROM [resulting_table]
GROUP BY clue_id
ORDER BY [something]
Any help is appreciated: what queries would fit this scenario?
The first part of your question can be solved this way (it expects that run_order is unique per group):
SELECT t1.group_id,
t1.clue_id AS current_clue
FROM group_clue t1
INNER JOIN (SELECT group_id,
MIN(run_order) as run_order
FROM group_clue
WHERE completed = 0
GROUP BY group_id) t2 USING (group_id, run_order)
The logic of this query is pretty simple:
The inner query selects the pairs of group_id and the corresponding minimal value of run_order which has the completed = 0.
After that we join the original table to this set of pairs so that we could select the corresponding clue_id additionally.
You can sort by number of elements per group using
ORDER BY COUNT(*) DESC

Categories