how to increment 1 every row after get row id in sql - php

i have a one question for increment +1 in every row
i have a sql table in that table have order_status column when new record created order_status will be store +1 then older record in that case if i have order status like 0 1 2 3
but when user send me order status 2 for 5th record so in this case record no 5 will be store as a record status 2 but i need to change status aftr 2 like
2 will be 3, 3 will be 4, 4 will be 5
if its posible so please me sql query

Use an UPDATE query.
UPDATE yourTable
SET order_status = order_status + 1
WHERE order_status >= 2

Related

how can i update only 40 rows in a table and not more than 40

I have a table like below
table name student
id
student
status
date
1
Mr.1
0
11-2-2022
2
Mr.1
1
10-2-2022
3
Mr.2
0
10-2-2022
now i want to make update query in only 40 rows where student is equal to Mr.1 & status is equal to 0 and date is equal to current month then update status of 40 rows to 1. now if i run the query again
it will count the 40 rows again and check is there any rows contain 0 or not , if 39 rows contains 1 and only 1 rows contains 0 then it will update to only one rows.
this is my basic query
UPDATE student SET status= '1' WHERE student = 'Mr.1' ORDER BY id DESC LIMIT 40;

Use two columns auto increment ( laravel 4.1)

I have these tables
Hotels:
id = auto increment
name_hotel = text
....
Rooms:
id = auto increment
id_room = auto increment (just in same hotel)
hotel_id = integer
...
In room table I have three columns (id = auto increment , id_room = I want to make this column auto increment but just in 1 hotel , every hotel add room the id_room will be 1 , 2 ,3 ,4 )
Example:
id id_room hotel_id
1 1 4
2 2 4
3 3 4
4 1 (start again) 5(new hotel)
5 4 4
6 2 5
It possible to make it in laravel model Eloquent
Well, if you know the hotel ID when you insert your data, then you can set the id_room to be one more than the highest ID room of that hotel.
First, remove the auto increment from id_room. Auto increment is intended to be used with primary keys or on rows whose IDs should always increment. It is never a good idea to try and reset the counter and as far as I know Laravel does not give you the option. But by removing the auto_increment, we can create one ourselves that does what you need.
To do so, we can use Eloquent to select the max(id_room) where hotel_id=?:
$id = 4; // or whatever ID you are inserting
$current_id = DB::table('rooms')->where('hotel_id', $id)->max('id_room');
Then, just add one to the current_id when you insert your data
$row = DB::table('rooms')->insert(
['id_room' => $current_id + 1, 'hotel_id' => $id];
);

update rows with same column value mysql and delete one

TABLE cartitems
CartItemId|CartId|sku_id|prod_id|quantity
1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1
2 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1
I want to merger the rows having same sku_id and update the quantity and delete one duplicate row like this
CartItemId|CartId|sku_id|prod_id|quantity
1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 2
if delete doesnt happens its okay. i l manage it later.
it's simple Aggregate function with an ORDER BY Keyword to
use.
try,
select
min(CartItemId),
min(CartId),
sku_id,
Sum(quantity) from cartitems group by sku_id

Mysql update a row with another row value in same table

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

PHP MySQL Joins

Is there a way I could somehow do the following?
Table ONE
id c_id type
-------------------
1 1 7
2 2 7
3 3 5
4 4 7
Table TWO
id title live
-------------------
1 row1 1
2 row2 0
3 row3 0
4 row4 1
The c_id column links the data from table ONE to table TWO. Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2".
I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1.
Here's how I thought I'd do it, but this doesn't seem to work:
SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5
I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1.
You were close... try using an implicit join:
SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5
select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 5

Categories