Let's say I have in a table named threadloc:
id thread
4 1
3 2
2 3
1 4
for a table
I want to change the table value of thread so that I can pick any thread and put in on the bottom (id 1) and push all the other threads up one.
So like I pick 2 it would be:
id thread
4 1
3 3
2 4
1 2
UPDATE threadloc SET ID = ID + 1 WHERE thread <> #currentThread AND ID < #currentID;
UPDATE threadloc SET ID = 1 WHERE thread = #currentThread
edit: now it doesn't change higher IDs
Related
This is a pretty complicated database. So I have simplified the structure for easy understanding.
I have 2 tables
TABLE A : transactions
p_id rev_count
1 1
2 1
3 1
1 1
1 1
1 2 (Gets incremented as it has reached max entries)
2 1
2 1
2 2 (Gets incremented as it has reached max entries)
1 2
2 2
TABLE B : rev_countReference
p_id rev_count
1 2
2 2
3 1
So basically Table B stores the rev_count value as reference for TABLE A. Since p_id can accept only 3 transactions, whenever p_id goes exceeds 3 count the rev_count value is incremented and next p_id entry will have an incremented rev_count value.
QUESTION
I want to get number of transactions occupied in the maximum rev_count by each p_id.
Desired Output :
p_id used_rev
1 1
2 2
3 1
Tried so far :
Unfortunately i'm not able to write a code for the above DB example yet. But what I have tried so far with actual code.
public function getfreeslots($user_id) {
$this->db->select('count(t.id), t.reinvest_count, MAX(t.reinvest_count) AS max_reinvest, COUNT(t.id) AS outstandingSlots');
$this->db->from('transactions t');
$this->db->join('reinvestCount rc', "t.receiver_id=rc.user_id AND t.phase_id=rc.phase_id",'LEFT');
$this->db->where('t.receiver_id', $user_id);
$this->db->having('t.reinvest_count >= max_reinvest');
$this->db->group_by('t.phase_id');
$this->db->order_by('t.phase_id', 'ASC');
$query = $this->db->get();
$row = $query->result();
if (empty($row))
return FALSE;
return $row;
}
I'm using Codeigniter query builder. So answer using query builder will be much appreciated.
I have the following table (called node_items):
id node_id position
== ======= ========
1 1 1
2 1 1
3 2 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
9 3 1
10 3 1
The position field is supposed to mark an items position in the node. They are currently all set at 1. I am wanting to fix this table by setting an incremental position for each item per node_id. So, the result I am after is:
id node_id position
== ======= ========
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 2 4
7 2 5
8 3 1
9 3 2
10 3 3
So I somehow need to group each item by node_id and set the position to an initial value of 1 and increment it for each subsequent item with that node_id.
Note: To keep my explanation simple, the above node_ids are shown in order (1,1, 2, 2,2,2,2,3,3,3), but this isn't usually the case.
What query can I use to update the position values incrementally for each node_id?
Fixing the table once can be done with an UPDATE:
SET #n = 0, #p = 1;
UPDATE node_items
SET position = (#p := IF(node_id=#n, #p+1, 1)),
node_id = (#n := node_id)
ORDER BY node_id, id;
Making the table maintain the position values as you insert/update/delete data is harder. Basically, can't do it while allowing concurrent updates. You have to lock the table in every session that needs to do writes to the table. This makes concurrent sessions run serially.
You can read my old post about this here: Some sort of “different auto-increment indexes” per a primary key values
I have a table like this
id Bill No Branches_id
1 1 1
2 2 1
3 3 1
4 1 2
5 4 1
when user deletes bill no 2 all other record's bill_no update in numerical order like this
id Bill No Branches_id
1 1 1
3 2 1
4 1 2
5 3 1
is there any easy way to reorder all records without a loop in programmatically
It appears that id is the primary key and you want to update another column within each bill. This can be reasonable.
You can use variables:
declare #rn := 0;
update likethis lt
set bill_no = (#rn := #rn + 1)
where lt.branches_id = 1
order by lt.id;
Note that all foreign references to the table should be using id. If bill_no is used to connect to another table, then you shouldn't change the value (even with cascading foreign keys), unless this is a very rare occurrence.
I have a table of people, around 50K+ records. I need to add a new field 'given_number' to this table that defaults to an alternating 1, 2 or 3.
e.g.
id 1 - given_number = 1
id 2 - given_number = 2
id 3 - given_number = 3
id 4 - given_number = 1
id 5 - given_number = 2
id 6 - given_number = 3
id 7 - given_number = 1
id 8 - given_number = 2
id 9 - given_number = 3
What's the best way of doing this?
Being given_number always a calculation you can make from id (id plus 1 modulo 3 plus 1), I think you don't need to actually have that field in the table, but ask for that calculation whenever you need it, and name that calculation given_number:
SELECT (id+1)%3+1 AS given_number, ... FROM ...
I will suggest you to add column first then by using sql you need to set the value for newly added field
I don't think i have a best solution but may be you can try this:
Add column with default set to 1
then update and increment all rows by 1 whose Id is divisible by 2
in same way increment all rows by whose id is divisible by 3.
I have a table with main-ids and user-ids. Each user-id has a set of their own unique main-ids, but multiple user-ids can have the same main-id. Is there anyway to increment a main-id for a specific user without having to do 2 queries?
If you mean like this:
User ID Main ID
1 1
1 2
1 3
2 1
2 2
2 3
Then you're going to need to make an INSERT trigger that finds the next MainID for that user and stores that.