I have the table posts, where id_post is primary key & autoincrement
id_post | post
1 hi1
3 hi3
27 hi27
45 hi45
67 hi67
69 hi69
I want to update id_post in order it to get its row position in the table
id_post | post
1 hi1
2 hi3
3 hi27
4 hi45
5 hi67
6 hi69
How could it be done?
I was thinking using ROW_NUMBER()
Run these commands:
SET #x = 0;
ALTER TABLE `posts` AUTO_INCREMENT = 1;
UPDATE `posts` SET `id_post` = #x:= #x + 1;
You can use set oprion as bellow :
before
+--------+--------+
|id_post | itemID |
+--------+--------+
| 1 | hi1 |
| 3 | hi3 |
| 27 | hi27 |
| 45 | hi45 |
| 67 | hi67 |
| 69 | hi69 |
+--------+--------+
after
--------------------
mysql> SET #nbr=0;
Query OK, 0 rows affected (0.00 sec)
mysql> update posts set id=#nbr:=#nbr+1
+--------+--------+
|id_post | post |
+--------+--------+
| 1 | hi1 |
| 2 | hi3 |
| 3 | hi27 |
| 4 | hi45 |
| 5 | hi67 |
| 6 | hi69 |
+--------+--------+
6 rows in set (0.00 sec)
Related
I have table like this
user_id workplace
1 Microsoft
1 Google
2 eBay
3 Panadora
3 Netflix
What I want is to have a table like this:
user_id places_worked
1 Microsoft,Google
2 eBay
3 Panadora,Netflix
Is there anyway in SQL that can do this?
As mentioned by #jarlh you could do this using group_concat
SELECT user_id,GROUP_CONCAT(workplace)
FROM yourtable
GROUP BY user_id;
you can use group by with group concat operation
SELECT user_id,GROUP_CONCAT(workplace)
FROM yourtable
GROUP BY user_id;
check following example
select * from payments;
+----+------------+---------+-------+
| id | date | user_id | value |
+----+------------+---------+-------+
| 1 | 2016-06-22 | 1 | 10 |
| 2 | 2016-06-22 | 3 | 15 |
| 3 | 2016-06-22 | 4 | 20 |
| 4 | 2016-06-23 | 2 | 100 |
| 5 | 2016-06-23 | 1 | 150 |
+----+------------+---------+-------+
5 rows in set (0.00 sec)
select c.user_id,group_concat(p.value) from calls c inner join payments p on p.user_id=c.user_id group by c.user_id;
+---------+-----------------------+
| user_id | group_concat(p.value) |
+---------+-----------------------+
| 1 | 10,150,10,150,10,150 |
| 2 | 100 |
+---------+-----------------------+
2 rows in set (0.00 sec)
I Have a MySQL DataBase which contains two tables linked with a foreign key:
MatPr(id, Designation, threshold_stock)
And
MvtStock_MatPr(id, Qte_Mvt, Qte_Tot,idMatPr)
What I want is to get the last Qte_tot, Designation,threshold_stock where threshold_stock>qte_tot
I tried this code and did not work:
SELECT MvtStock_MatPr.id,idMatPr, Nom, threshold_stock, Qte_Tot
FROM MvtStock_MatPr, MatPr
WHERE MatPr.id=MvtStock_MatPr.idMatPr AND threshold_stock>Qte_Tot
Here is Sample Data set:
mysql> SELECT MvtStock_MatPr.id,idMatPr, Designation, threshold_stock, Qte_Tot FROM MvtStock_MatPr, MatPr WHERE MatPr.id=MvtStock_MatPr.idMatPr AND threshold_stock>Qte_Tot ORDER
BY id,idMatPr;
| id | idMatPr | Dsignation| threshold_stock| Qte_Tot |
| 1 | 1 | bakra | 120 | 10 |
| 2 | 2 | zipper | 130 | 20 |
| 3 | 1 | bakra | 120 | 30 |
| 7 | 2 | zipper | 130 | 50 |
4 rows in set (0.00 sec)
mysql> SELECT * FROM MatPr;
| id | Designation| threshold_stock|
| 1 | bakra | 120 |
| 2 | zipper | 130 |
2 rows in set (0.00 sec)
mysql> SELECT * FROM MvtStock_MatPr;
| id | DateMvt | Qte_Mvt | Qte_Tot | idMatPr |
| 1 | 2016-01-01 | 10 | 10 | 1 |
| 2 | 2016-02-02 | 20 | 20 | 2 |
| 3 | 2016-03-03 | 20 | 30 | 1 |
| 4 | 2016-03-03 | 100 | 130 | 1 |
| 5 | 2016-03-03 | 50 | 180 | 1 |
| 6 | 2016-03-03 | 20 | 200 | 1 |
| 7 | 2016-03-05 | 30 | 50 | 2 |
7 rows in set (0.00 sec)
What I would like to get is:
| id | idMatPr | Dsignation| threshold_stock| Qte_Tot
| 3 | 1 | bakra | 120 | 30 |
| 7 | 2 | zipper | 130 | 50 |
+----+---------+--------+-------+---------+
Thanks for your contributions
Use the below query
Select m1.id,m1.idMatpr,m.threshold_stock,m.Qte_Tot
From MvtStock_Matpr m1 join Matpr m
On m1.idMatPr=m.id and m.threshold_stock>m1.Qte_Tot
I'm not sure that I completely follow what your problem is: is it selecting just one row from a result set? If so, does a subquery fix your problem?
SELECT *
FROM MatPr
WHERE ID = (
SELECT idMatPr
FROM MvtStock_MatPr, MatPr
WHERE MatPr.id = MvtStock_MatPr.idMatPr
AND threshold_stock > Qte_Tot
LIMIT 1
)
If you want to select just the rows from MatPr, does this work for you?
SELECT MatPr.*
FROM MvtStock_MatPr, MatPr
WHERE MatPr.id = MvtStock_MatPr.idMatPr
AND threshold_stock > Qte_Tot
http://sqlfiddle.com/#!9/d4ef50/2
Try this query:
select * from
( SELECT MvtStock_MatPr.id,idMatPr,Designation, threshold_stock, Qte_Tot
FROM MvtStock_MatPr join MatPr
on MatPr.id=MvtStock_MatPr.idMatPr
where threshold_stock>Qte_Tot
order by DateMvt desc
) T
group by T.idMatPr
This is my table with id field as unsigned tinyint PRIMARY KEY with NOT NULL and AUTO_INCREMENT.
+----+
| id |
+----+
| 1 |
| 4 |
|254 |
|255 |
+----+
My application is an embedded application so I am using the small datatypes like tinyint. My total count of id values wont go upto 255. But in case if the in between values are deleted for number of times then inserting the new id value will definitely reach 255 sooner or later.
I have some questions,
Is it possible to set the auto increment feature such that it will be insert the new id to the not existing greater value (here 2)?
If not please suggest some way to handle this issue other than using higher data type for id and updating the higher id values after deleting an id (like if I delete id = 2 update all the id values greater than 2 to id-1 so that all the values remain in sequence while inserting new value).
Append your fields for insert. this will use the lowest free id
INSERT INTO ai (id) (
SELECT a.id+1 FROM ai a
LEFT JOIN ai b ON a.id+1 = b.id
WHERE b.id IS NULL LIMIT 1
)
OR to get also 1
SELECT a.id+1 FROM (
SELECT 0 AS id UNION SELECT id FROM ai
) AS a
LEFT JOIN ai b ON a.id+1 = b.id
WHERE b.id IS NULL
ORDER by a.id ASC
LIMIT 1;
I can't think why you choose to be bound by this arbitrary limit. That said, if were to do this then I would construct a table with all possible integers (1-255) and a flag indicating whether the value was currently in use. So a DELETE becomes UPDATE x SET flag = 0 WHERE id = n. Then the query to find the lowest 0 flag becomes trivial.
#
Edge Goldberg - if you want to reorder the entrys and use auto_increment use this. Then also lst_insertid will work -- : -- After DELETE a ROW
UPDATE abc , (SELECT #nr:=0) AS INIT SET a := #nr := (#nr+1);
ALTER TABLE abc AUTO_INCREMENT=1;
-- INSERT a new one
INSERT INTO abc (a) VALUES(1234);
SELECT LAST_INSERT_ID();
Here is the Sample for set auto_increment
MariaDB []> select * from abc;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
| 00002 | 3 |
| 00004 | 5 |
| 00005 | 6 |
| 00007 | 8 |
| 00008 | 9 |
| 00009 | 10 |
| 00010 | 11 |
| 00012 | 13 |
| 00013 | 14 |
| 00014 | 15 |
| 00015 | 16 |
| 00016 | 17 |
| 00017 | 18 |
| 00018 | 19 |
| 00033 | 34 |
| 00077 | 78 |
| 00555 | 556 |
+-------+------+
18 rows in set (0.00 sec)
MariaDB []> -- After DELETE a ROW
MariaDB []> UPDATE abc , (SELECT #nr:=0) AS INIT SET a := #nr := (#nr+1);
Query OK, 16 rows affected (0.03 sec)
Rows matched: 18 Changed: 16 Warnings: 0
MariaDB []> ALTER TABLE abc AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB []> select * from abc;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
| 00002 | 3 |
| 00003 | 4 |
| 00004 | 5 |
| 00005 | 6 |
| 00006 | 7 |
| 00007 | 8 |
| 00008 | 9 |
| 00009 | 10 |
| 00010 | 11 |
| 00011 | 12 |
| 00012 | 13 |
| 00013 | 14 |
| 00014 | 15 |
| 00015 | 16 |
| 00016 | 17 |
| 00017 | 18 |
| 00018 | 19 |
+-------+------+
18 rows in set (0.00 sec)
MariaDB []> INSERT INTO abc (a) VALUES(NULL);
Query OK, 1 row affected (0.01 sec)
MariaDB []> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 19 |
+------------------+
1 row in set (0.00 sec)
MariaDB []> select * from abc;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
| 00002 | 3 |
| 00003 | 4 |
| 00004 | 5 |
| 00005 | 6 |
| 00006 | 7 |
| 00007 | 8 |
| 00008 | 9 |
| 00009 | 10 |
| 00010 | 11 |
| 00011 | 12 |
| 00012 | 13 |
| 00013 | 14 |
| 00014 | 15 |
| 00015 | 16 |
| 00016 | 17 |
| 00017 | 18 |
| 00018 | 19 |
| 00019 | 20 |
+-------+------+
19 rows in set (0.01 sec)
MariaDB []>
I have data that has to be dumped into a mysql table. I am depending on a Bill_Number to identify the records. But for few records, the billing number is same; it is because either the customer returned it or cancelled it or something.
Now I want to generate some key for these transactions.
For example, when an ID '123456' appears first time, I want to give it a key '98765'. When the same ID 123456 comes again, I want to give it a key '98765-1' and so on.
here ist your Answer. Now you can insert every Row and field in a new Table
MariaDB [tmp]> select * from example;
+----+--------+
| id | billnr |
+----+--------+
| 1 | 6789 |
| 2 | 1234 |
| 3 | 6789 |
| 4 | 6789 |
| 5 | 2345 |
| 6 | 1234 |
+----+--------+
6 rows in set (0.00 sec)
MariaDB [tmp]> SELECT
-> #nr:=IF(#lastid = e.billnr , #nr:=#nr+1 ,0) AS recno,
-> #lastid:=billnr AS last_type,
-> concat(billnr,IF(#nr > 0, concat('-',#nr),'')) AS new_billnr,
-> e.*
-> FROM `example`e,
-> (SELECT #lastid:=0, #nr:=0) tmp
-> ORDER BY billnr;
+-------+-----------+------------+----+--------+
| recno | last_type | new_billnr | id | billnr |
+-------+-----------+------------+----+--------+
| 0 | 1234 | 1234 | 2 | 1234 |
| 1 | 1234 | 1234-1 | 6 | 1234 |
| 0 | 2345 | 2345 | 5 | 2345 |
| 0 | 6789 | 6789 | 1 | 6789 |
| 1 | 6789 | 6789-1 | 3 | 6789 |
| 2 | 6789 | 6789-2 | 4 | 6789 |
+-------+-----------+------------+----+--------+
6 rows in set (0.00 sec)
MariaDB [tmp]>
I have 4 tables:
ARTICOLE
BAR
BUCATARIE
MAGAZIE
mysql> select * from ARTICOLE;
| OID | ART |
| 1 | TEST |
| 2 | TESTQ |
| 3 | MYART |
| 4 | MYARTBUC |
4 rows in set (0.00 sec)
mysql> select * from BAR;
| OID | ART | CANT |
| 1 | TEST | 3.00000 |
| 2 | TESTQ | 1.00000 |
| 3 | MYART | 20.00000 |
3 rows in set (0.00 sec)
mysql> select * from BUCATARIE;
| OID | ART | CANT |
| 1 | TEST | 5.00000 |
| 2 | MYARTBUC | 10.00000 |
2 rows in set (0.00 sec)
mysql> select * from MAGAZIE;
Empty set (0.00 sec)
the below query
mysql> select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT) from ARTICOLE a,BUCATARIE buc,BAR bar,MAGAZIE mag where a.ART=bar.ART and a.ART=bar.ART and a.ART=mag.ART group by a.ART;
return:
Empty set (0.00 sec)
how must be query to return:
| ART | sum(bar.CANT) | sum(buc.CANT) | sum(mag.CANT) |
TEST | 3.00000 | 5.00000 | NULL |
TESTQ | 1.00000 | NULL | NULL |
MYART | 20.00000 | NULL | NULL|
MYARTBUC | NULL | 10.00000 | NULL |
????
Any help appreciated.
You need to make use of LEFT JOIN to include results from other tables without filtering out records that don't match:
select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT)
from ARTICOLE a
left join BUCATARIE buc on buc.ART = a.ART
left join BAR bar on bar.ART = a.ART
left join MAGAZIE mag on mag.ART = a.ART
group by a.ART;
Sample results:
ART SUM(BAR.CANT) SUM(BUC.CANT) SUM(MAG.CANT)
MYART 20
MYARTBUC 10
TEST 3 5
TESTQ 1
Demo: http://www.sqlfiddle.com/#!2/6efb2/2