I am using mysql and need to duplicate all row in my table but I do not duplicate id column and new generate number
need ex.
my table
item_db
+------------------------------------------+
| id | name | price | detail |
+------------------------------------------+
| 1 | example | 230 | 3 |
| 2 | power | 110 | 3 |
| 3 | voltage | 1.2 | 4 |
| 4 | example | 240 | 4 |
| 5 | example | 320 | 6 |
| 6 | power | 100 | 4 |
| 7 | power | 110 | 6 |
| 8 | example | 230 | 3 |
| 9 | power | 110 | 3 |
| 10 | voltage | 1.2 | 4 |
| 20 | example | 240 | 4 |
| 21 | example | 320 | 6 |
| 22 | power | 100 | 4 |
| 23 | power | 110 | 6 |
| 24 | example | 240 | 4 |
| 25 | example | 320 | 6 |
| 26 | power | 100 | 4 |
| 27 | power | 110 | 6 |
| 28 | example | 240 | 4 |
| 29 | example | 320 | 6 |
| 30 | power | 100 | 4 |
+------------------------------------------+
i need to duplicate
item_db
+------------------------------------------+
| id | name | price | detail |
+------------------------------------------+
| 1 | example | 230 | 3 |
| 2 | power | 110 | 3 |
| 3 | voltage | 1.2 | 4 |
| 4 | example | 240 | 4 | I need to dup id 1-10 to 101-110
| 5 | example | 320 | 6 |
| 6 | power | 100 | 4 |
| 7 | power | 110 | 6 |
| 8 | example | 230 | 3 |
| 9 | power | 110 | 3 |
| 10 | voltage | 1.2 | 4 |
| 101| example | 230 | 3 |
| 102| power | 110 | 3 |
| 103| voltage | 1.2 | 4 |
| 104| example | 240 | 4 |
| 105| example | 320 | 6 |
| 106| power | 100 | 4 |
| 107| power | 110 | 6 |
| 108| example | 230 | 3 |
| 109| power | 110 | 3 |
| 110| voltage | 1.2 | 4 |
| 20 | example | 240 | 4 |
| 21 | example | 320 | 6 |
| 22 | power | 100 | 4 |
| 23 | power | 110 | 6 |
| 24 | example | 240 | 4 |
| 25 | example | 320 | 6 |
| 26 | power | 100 | 4 | i need to dup 20-30 to 200-210
| 27 | power | 110 | 6 |
| 28 | example | 240 | 4 |
| 29 | example | 320 | 6 |
| 30 | power | 100 | 4 |
| 200| example | 240 | 4 |
| 201| example | 320 | 6 |
| 202| power | 100 | 4 |
| 203| power | 110 | 6 |
| 204| example | 240 | 4 |
| 205| example | 320 | 6 |
| 206| power | 100 | 4 |
| 207| power | 110 | 6 |
| 208| example | 240 | 4 |
| 209| example | 320 | 6 |
| 210| power | 100 | 4 |
+------------------------------------------+
thanks you expert.
You can use INSERT ... SELECT:
INSERT INTO item_db (id, name, price, detail)
SELECT id+180, name, price, detail FROM item_db WHERE id BETWEEN 20 AND 30
How exactly you want to treat id is not entirely clear. If, for example, you wish to automatically assign AUTO_INCREMENT values, then you can (as usual) omit it from the INSERT statement.
Related
I have a table like this in MS Access 2019:
+-----------+------------+--------+----------+-------+
| BillingID | Date | RoomID | Electric | Water |
+-----------+------------+--------+----------+-------+
| 1 | 12/23/2018 | 4 | 1669 | 106 |
| 2 | 12/26/2018 | 1 | 5035 | 289 |
| 3 | 12/27/2018 | 6 | 0 | 0 |
| 4 | 12/31/2018 | 5 | 3158 | 223 |
| 5 | 1/6/2019 | 2 | 3823 | 194 |
| 6 | 1/15/2019 | 3 | 1772 | 125 |
| 7 | 1/23/2019 | 4 | 1796 | 117 |
| 8 | 1/26/2019 | 1 | 5231 | 299 |
| 9 | 1/27/2019 | 6 | 0 | 0 |
| 10 | 1/31/2019 | 5 | 3366 | 242 |
| 11 | 2/14/2019 | 2 | 3975 | 201 |
| 12 | 2/15/2019 | 3 | 1839 | 129 |
+-----------+------------+--------+----------+-------+
I could calculate the electricity and water usage with Index & Match in MS Excel. However, I've had a lot of trouble to achieve this with MS Access. The result I want is as below:
+-----------+------------+--------+----------+---------------+-------+------------+
| BillingID | Date | RoomID | Electric | ElectricUsage | Water | WaterUsage |
+-----------+------------+--------+----------+---------------+-------+------------+
| 1 | 12/23/2018 | 4 | 1669 | | 106 | |
| 2 | 12/26/2018 | 1 | 5035 | | 289 | |
| 3 | 12/27/2018 | 6 | 0 | | 0 | |
| 4 | 12/31/2018 | 5 | 3158 | | 223 | |
| 5 | 1/6/2019 | 2 | 3823 | | 194 | |
| 6 | 1/15/2019 | 3 | 1772 | | 125 | |
| 7 | 1/23/2019 | 4 | 1796 | 127 | 117 | 11 |
| 8 | 1/26/2019 | 1 | 5231 | 196 | 299 | 10 |
| 9 | 1/27/2019 | 6 | 0 | | 0 | |
| 10 | 1/31/2019 | 5 | 3366 | 208 | 242 | 19 |
| 11 | 2/14/2019 | 2 | 3975 | 152 | 201 | 7 |
| 12 | 2/15/2019 | 3 | 1839 | 67 | 129 | 4 |
+-----------+------------+--------+----------+---------------+-------+------------+
For example, for RoomID = 4, the ElectricUsage is the difference between the Electric in BillingID #7 and BillingID #1 and so on.
I've tried some answer like this or this but Access ran into errors when using those solutions in SQL view (Syntax error in FROM clause).
Thanks.
You can use a couple of sub-queries to return the Electric/Water for each room on the previous date:
SELECT
B.BillingID, B.BillingDate, B.RoomID, B.Electric,
B.Electric-(SELECT TOP 1 E.Electric FROM tblBilling AS E WHERE B.RoomID=E.RoomID AND E.BillingDate<B.BillingDate ORDER BY E.BillingDate DESC) AS ElectricUsage,
B.Water,
B.Water-(SELECT TOP 1 W.Water FROM tblBilling AS W WHERE B.RoomID=W.RoomID AND W.BillingDate<B.BillingDate ORDER BY W.BillingDate DESC) AS WaterUsage
FROM tblBilling AS B
Note that I've renamed your Date field to be BillingDate, as Date is a reserved word in Access, and will cause you problems in the future.
Regards,
I have four tables, three of which I need data from, as well as a fourth that stores how this data is associated. When running my query, I'm getting the wrong output and it seems like I'm not linking things up correctly, but I'm unsure as to how to do so. How do I link my joins to get the output I'm looking for?
So far, I've used
SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id=pc.player_id
JOIN classes c ON c.class_id=pc.class_id
JOIN specialization s ON s.spec_id=pc.spec_id
which references
CREATE TABLE players(
player_id INT UNSIGNED auto_increment PRIMARY KEY,
name VARCHAR(20)
)...;
CREATE TABLE classes(
class_id INT UNSIGNED auto_increment PRIMARY KEY,
class VARCHAR(20)
)...;
CREATE TABLE specializations(
spec_id INT UNSIGNED auto_increment PRIMARY KEY,
class_id INT UNSIGNED,
specialization VARCHAR(20)
)...;
but I'd like to be using this table in some way to display the information correctly linked, I'm just unsure how to do so:
CREATE TABLE players_classes(
pc_id INT UNSIGNED auto_increment PRIMARY KEY,
FOREIGN KEY(class_id) REFERENCES classes(class_id),
FOREIGN KEY(player_id) REFERENCES players(player_id),
FOREIGN KEY(spec_id) REFERENCES specializations(spec_id)
)...;
I'm expecting to be able to grab a player's name as well as their associated class and specialization, actual results are showing the values associated with 1/1/1 for each id, and so on and so forth.
edit: the data from players_classes is as follows
pc_id | class_id | player_id | spec_id |
1 3 1 8
2 12 2 35
3 2 3 6
etc.
therefore the expected result from this is
class | name | spec
paladin seranul holypa
hunter contherious marksmanship
priest unicorns holypr
I am instead getting
class | name | spec
warlock Affliction Affliction
warlock Grireaver Demonology
warlock Affliction Demonology
and so on throughout the table, listing combinations that do not exist within my players_classes table
specializations table
+---------+----------+----------------+
| spec_id | class_id | specialization |
+---------+----------+----------------+
| 1 | 1 | Affliction |
| 2 | 1 | Destruction |
| 3 | 1 | Demonology |
| 4 | 2 | Shadow |
| 5 | 2 | Discipline |
| 6 | 2 | HolyPr |
| 7 | 3 | Retribution |
| 8 | 3 | HolyPa |
| 9 | 3 | ProtectionPa |
| 10 | 4 | ProtectionWa |
| 11 | 4 | Arms |
| 12 | 4 | Fury |
| 13 | 5 | FrostMa |
| 14 | 5 | Fire |
| 15 | 5 | Arcane |
| 16 | 6 | vengeance |
| 17 | 6 | havoc |
| 18 | 7 | guardian |
| 19 | 7 | balance |
| 20 | 7 | feral |
| 21 | 7 | restorationDr |
| 22 | 8 | elemental |
| 23 | 8 | enhance |
| 24 | 8 | restorationSh |
| 25 | 9 | frostDk |
| 26 | 9 | blood |
| 27 | 9 | unholy |
| 28 | 10 | outlaw |
| 29 | 10 | assassin |
| 30 | 10 | subtlety |
| 31 | 11 | brewmaster |
| 32 | 11 | windwalker |
| 33 | 11 | mistweaver |
| 34 | 12 | BeastMaster |
| 35 | 12 | marksmanship |
| 36 | 12 | Survival |
+---------+----------+----------------+
classes table
+----------+-------------+
| class_id | class |
+----------+-------------+
| 1 | warlock |
| 2 | priest |
| 3 | paladin |
| 4 | warrior |
| 5 | mage |
| 6 | demonhunter |
| 7 | druid |
| 8 | shaman |
| 9 | deathknight |
| 10 | rogue |
| 11 | monk |
| 12 | hunter |
+----------+-------------+
players table
+-----------+--------------+
| player_id | name |
+-----------+--------------+
| 1 | Seranul |
| 2 | Contherious |
| 3 | Unicorns |
| 4 | Remereili |
| 5 | Affliction |
| 6 | Meowing |
| 7 | Brobot |
| 8 | Bagelsbbq |
| 9 | Rafusen |
| 10 | Taiboku |
| 11 | Yikes |
| 12 | Thunderblaze |
| 13 | Muo |
| 14 | Intz |
| 15 | Trunks |
| 16 | Kalphyte |
| 17 | Eyeoftheshoe |
| 18 | Amuhnet |
| 19 | Synkka |
| 20 | Affliction |
| 21 | Kts |
| 22 | Shadowdreams |
| 23 | Zahel |
| 24 | Azrama |
| 25 | Seranul |
| 26 | Momspaghetti |
| 27 | Ohki |
| 28 | Rafusen |
| 29 | Cindyy |
| 30 | Grireaver |
| 31 | Intz |
| 32 | lazy |
| 33 | missworld |
| 34 | Affliction |
| 35 | Amuhnet |
| 36 | eyeoftheshoe |
| 37 | sanctus |
| 38 | nozshelen |
| 39 | Contherious |
| 40 | messer |
| 41 | catathor |
| 42 | demonblaze |
| 43 | wrillett |
| 44 | raagnnar |
| 45 | xizi |
| 46 | nemesix |
| 47 | zeroskill |
| 48 | chikfillidan |
| 49 | tentenlol |
| 50 | unicorns |
| 51 | bubuhtide |
| 52 | ohki |
| 53 | azrama |
+-----------+--------------+
players_classes table
+-------+----------+-----------+---------+
| pc_id | class_id | player_id | spec_id |
+-------+----------+-----------+---------+
| 1 | 3 | 1 | 8 |
| 2 | 12 | 2 | 35 |
| 3 | 2 | 3 | 6 |
| 4 | 11 | 4 | 31 |
| 5 | 1 | 5 | 1 |
| 6 | 12 | 6 | 34 |
| 7 | 2 | 7 | 6 |
| 8 | 11 | 8 | 31 |
| 9 | 2 | 9 | 6 |
| 10 | 7 | 10 | 21 |
| 11 | 4 | 11 | 11 |
| 12 | 8 | 12 | 22 |
| 13 | 4 | 13 | 12 |
| 14 | 5 | 14 | 13 |
| 15 | 3 | 15 | 7 |
| 16 | 11 | 16 | 33 |
| 17 | 8 | 17 | 22 |
| 18 | 2 | 18 | 6 |
| 19 | 8 | 19 | 23 |
| 20 | 11 | 20 | 33 |
| 21 | 5 | 21 | 13 |
| 22 | 6 | 22 | 17 |
| 23 | 10 | 23 | 29 |
| 24 | 8 | 24 | 22 |
| 25 | 11 | 25 | 31 |
| 26 | 11 | 26 | 32 |
| 27 | 4 | 27 | 11 |
| 28 | 5 | 28 | 13 |
| 29 | 7 | 29 | 19 |
| 30 | 1 | 30 | 3 |
| 31 | 9 | 31 | 25 |
| 32 | 3 | 32 | 8 |
| 33 | 9 | 33 | 25 |
| 34 | 1 | 34 | 3 |
| 35 | 2 | 35 | 6 |
| 36 | 4 | 36 | 11 |
| 37 | 5 | 37 | 13 |
| 38 | 5 | 38 | 13 |
| 39 | 9 | 39 | 25 |
| 40 | 6 | 40 | 17 |
| 41 | 10 | 41 | 28 |
| 42 | 2 | 42 | 6 |
| 43 | 8 | 43 | 24 |
+-------+----------+-----------+---------+
You need to use the relationship table in your query:
SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id = pc.player_id
JOIN classes c ON c.class_id = pc.class_id
JOIN specializations s ON s.spec_id = pc.spec_id
DEMO
You shouldn't even have class_id and spec_id in the other tables, since these are many-to-many relationships, and those columns can only be used for one-to-one relationships.
i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;
first table :
table Name : ssc_course;
+---------------+-----------------+------------+
| ssc_course_id | ssc_course_name | qualify_id |
+---------------+-----------------+------------+
| 1 | HSC | 1 |
| 2 | Diploma | 1 |
| 3 | ITI | 1 |
+---------------+-----------------+------------+
second table :
table name :ssc_stream
+---------------+--------------------+-----------------+
| ssc_stream_id | ssc_stream_name | ssc_course_id(fk) |
+---------------+--------------------+-----------------+
| 1 | Science | 1 |
| 2 | Commers | 1 |
| 3 | Arts | 1 |
| 17 | Computer Engg | 2 |
| 18 | Mechanical Engg | 2 |
| 19 | Civil Engg | 2 |
| 20 | Electronics & TELE | 2 |
| 21 | E&TC | 2 |
+---------------+--------------------+-----------------+
third table :(master table)
table name : ssc_college
+------------+--------------+---------------+---------------+-
| ssc_clg_id | clg_login_id | ssc_stream_id | ssc_course_id |
+------------+--------------+---------------+---------------+-
| 9 | 2 | 1 | 1 |
| 10 | 2 | 1 | 1 |
| 11 | 2 | 2 | 1 |
| 12 | 2 | 2 | 1 |
| 13 | 2 | 3 | 1 |
| 14 | 2 | 3 | 1 |
| 15 | 3 | 1 | 1 |
| 16 | 3 | 1 | 1 |
| 17 | 3 | 2 | 1 |
| 18 | 3 | 2 | 1 |
| 19 | 3 | 3 | 1 |
| 20 | 3 | 3 | 1 |
| 21 | 4 | 1 | 1 |
| 22 | 4 | 1 | 1 |
| 23 | 4 | 2 | 1 |
| 24 | 4 | 2 | 1 |
| 25 | 4 | 3 | 1 |
| 26 | 4 | 3 | 1 |
| 27 | 5 | 17 | 2 |
| 28 | 5 | 17 | 2 |
| 29 | 5 | 18 | 2 |
| 30 | 5 | 18 | 2 |
| 31 | 5 | 19 | 2 |
| 32 | 5 | 19 | 2 |
| 33 | 5 | 20 | 2 |
| 34 | 5 | 20 | 2 |
| 35 | 5 | 21 | 2 |
| 36 | 5 | 21 | 2 |
| 38 | 6 | 17 | 2 |
| 39 | 6 | 17 | 2 |
| 40 | 6 | 18 | 2 |
*************************************************************
I am trying to save course,stream id's into ssc_college table.
1 course have multiple stream how can i insert this type of values
ex. course 1-diploma and this course have multiple stream like-
1.comp. engg , 2.mechanical ,3.e&tc i want to add course id and
stream id at a time of third (ssc_college) table.
and multiple stream for multiple course how i can add at a time
multiple parent and multiple child id in mysql table.
Note: stream are dependant of course table.
good morning. I have this table:
mysql> select * from Data;
+---------------------------+--------+-------+
| affyId | exptId | level |
+---------------------------+--------+-------+
| 31315_at | 3 | 250 |
| 31324_at | 3 | 91 |
| 31325_at | 1 | 191 |
| 31325_at | 2 | 101 |
| 31325_at | 4 | 51 |
| 31325_at | 5 | 71 |
| 31325_at | 6 | 31 |
| 31356_at | 3 | 91 |
| 31362_at | 3 | 260 |
| 31510_s_at | 3 | 257 |
| 5321_at | 4 | 90 |
| 5322_at | 4 | 90 |
| 5323_at | 4 | 90 |
| 5324_at | 3 | 57 |
| 5324_at | 4 | 90 |
| 5325_at | 4 | 90 |
| AFFX-BioB-3_at | 3 | 97 |
| AFFX-BioB-5_at | 3 | 20 |
| AFFX-BioB-M_at | 3 | 20 |
| AFFX-BioB-M_at | 5 | 214 |
| AFFX-BioB-M_at | 7 | 20 |
| AFFX-BioB-M_at | 8 | 40 |
| AFFX-BioB-M_at | 9 | 20 |
| AFFX-HSAC07/X00351_M_at | 3 | 86 |
| AFFX-HUMBAPDH/M33197_3_st | 3 | 277 |
| AFFX-HUMTFFR/M11507_at | 3 | 90 |
| AFFX-M27830_3_at | 3 | 271 |
| AFFX-MurIL10_at | 3 | 8 |
| AFFX-MurIL10_at | 5 | 8 |
| AFFX-MurIL10_at | 6 | 4 |
| AFFX-MurIL2_at | 3 | 20 |
| AFFX-MurIL4_at | 5 | 78 |
| AFFX-MurIL4_at | 6 | 20 |
| U95-32123_at | 1 | 128 |
| U95-32123_at | 2 | 128 |
| U98-40474_at | 1 | 57 |
| U98-40474_at | 2 | 57 |
+---------------------------+--------+-------+
37 rows in set (0.00 sec)
If I wanna look for the average expression level (level) of each array probe (affyId) across all experiments, I do SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId;
However, I can't figure out how to look for the average expression level of each array probe (affyId) for each experiment... It must be something similar to the last query, but I don't obtain good results... any help?
PD: someone told me I should give some reputation or click to some green button if somebody solves my question... Is it right? How do I do it? I'm pretty new on this website...
This shows the average for every affyId:
SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId
This the average for every exptId:
SELECT exptId, AVG(level) AS average FROM Data GROUP BY exptId
and this the average for every exptId in every affyId:
SELECT affyId, exptId, AVG(level) AS average FROM Data GROUP BY exptId, affyId
Just add that to the group by clause
SELECT affyId, exptId, AVG(level) AS average
FROM Data
GROUP BY affyId, exptId;