I posted another question that is similar.
So still having more problems.
This is the case, I want to be able to update the column position.
following the example below. here is before the update
+----+--------------+----------+
| id | question | position |
+----+--------------+----------+
| 1 | Question 1 | 1 |
| 2 | Question 2 | 2 |
| 3 | Question 3 | 3 |
| 4 | Question 4 | 4 |
+----+--------------+----------+
So If I update the row id = 2 the column position from 2 to 4, I want to be organized as the following. here is after the update
+----+--------------+----------+
| id | question | position |
+----+--------------+----------+
| 1 | Question 1 | 1 |
| 2 | Question 2 | 4 |
| 3 | Question 3 | 2 |
| 4 | Question 4 | 3 |
+----+--------------+----------+
I tried many thing and nothing works.
So if anyone could help me will be very apprecciated.
Thanks a lot.
You want to do something like this:
update t join
(select position from t where id = 2) t1
on t.position >= t1.position
set position = (case when id = 2 then 4 else position - 1 end)
That is, subtract "1" from all positions greater than the one with id = 2. And set that position to "4".
Related
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;
I am making a management system for a site that has a bunch of image galleries. The interface will allow the user to add, delete, or reorder images within each gallery.
I have a table for all the images across all galleries together, with 'id' auto-incremented, and default for sort_order set to 0.
+----+-----------+------------+------------+
| id |gallery_id | sort_order |
+----+-----------+------------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 2 |
| 4 | 1 | 3 |
| 5 | 1 | 4 |
| 6 | 1 | 5 |
| 7 | 1 | 6 |
| 8 | 2 | 0 |
| 9 | 2 | 1 |
| 10 | 2 | 2 |
| 11 | 2 | 3 |
| 12 | 2 | 4 |
| 13 | 2 | 5 |
| 14 | 2 | 6 |
+----+-----------+------------+------------+
Here is the reorder query, using a serialized array via ajax:
if($_POST['item']) {
$order = 0;
foreach ($_POST['item'] as $id) {
$rearrange = $db->query
("UPDATE images SET sort_order = '".$order."' WHERE id = '".$id."'");
$order++;
It limits sort_order according to the length of the array that is passed, and then defaults to 0 for the next group its comes to. I can reorder, add, delete, and it always restricts changes to the gallery_id group I am editing.
(As an experiment, I inserted sort_order values ascending across all galleries, like this:
+----+-----------+------------+------------+
| id |gallery_id | sort_order |
+----+-----------+------------+------------+
| 1 | 1 | 0 |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 5 |
| 6 | 1 | 6 |
| 7 | 1 | 7 |
| 8 | 2 | 8 |
| 9 | 2 | 9 |
| 10 | 2 | 10 |
| 11 | 2 | 11 |
| 12 | 2 | 12 |
| 13 | 2 | 13 |
| 14 | 2 | 14 |
+----+-----------+------------+------------+
And when I reordered rows using my interface, it limited the sort_order within gallery_id groups, exactly as shown in the first table).
I'm looking for caveats to this approach. Anyone done it this way?
UPDATE images
SET
sort_order = FIELD(id, A, B, C, D, E)
WHERE gallery_id = N
AND id IN(A, B, C, D, E)
I do it the same way, but I actually put the image inserted last by calculating the number of rows in each gallery. When a user wants to order he has to filter the list for a specific gallery first then he can sort the images in the gallery.
I cannot but point out the terrible security issues with your code. I really, really, really hope that you are not actually writing the queries like that as anybody can easily do whatever they want with your database.
http://xkcd.com/327/ comes to mind. If I submit an item like 0'; drop table images; your entire table is gone.
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
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
Hey guys...I am trying to get some data based on a condition which I don't know how to write the code for.
My table looks like this:
id | meta_id | key | value
-- ------- --- -----
1 | 1 | image | 0
2 | 1 | path | 1
3 | 1 | location | parent
4 | 2 | image | 1
5 | 2 | path | 2
6 | 2 | location | self
From this how can i return the meta_id that has key = image and value = 1 and value = 2..so you see i have value 2 times...can this be done?
I hope I've understood
select * from table where key = 'image' and value in (1,2)