Adding multiple rows mysql in phpmyadmin - mysql

I'm trying to execute a rather complicated task in phpmyadmin.
Shortly i need to add multiple rows in a table that uses a certain value from another table column.
I kinda figured out how to add 1 row manually) but since i need 11.000 of it..
INSERT INTO `DATABASE`.`ratings` (
`id` ,
`total_votes` ,
`total_value` ,
`used_ips`
)
VALUES (
'5000', '10', '1000', NULL
);
There are 2 TABLES (CONTENT and RATINGS) in same DB.
In CONTENT there is a column named CONTENT_RECORD (11.000 entries) which excists of only numbers
In RATINGS we have 4 colums (ID,TOTAL_VOTES,TOTAL_VALUE,USED_IPS)
I want to add multiple rows in RATING with following values.
ID = should copy the value from table CONTENT, column CONTENT_RECORD
Total_votes = fixed number 10
Total_value = fixed number 1000
used_ips = leave empty
Help would be much appreciated.

You can use insert..into..select syntax. Something like this -
INSERT INTO `DATABASE`.`ratings`(`id`, `total_votes`, `total_value`, `used_ips`)
SELECT CONTENT_RECORD, '10', '1000', NULL FROM CONTENT

insert into table_name
values('values of your table for row1'),('values of your table for row 2),..........('values of your table for row n

Related

MYSQL: How to update unique random number to existing rows

It's been my first question to this website, I'm sorry if I used any wrong keywords. I have been with one problem from quite a few days.
The Problem is, I have a MYSQL table named property where I wanted to add a ref number which will be a unique 6 digit non incremental number so I alter the table to add a new column named property_ref which has default value as 1.
ALTER TABLE property ADD uniqueIdentifier INT DEFAULT (1) ;
Then I write a script to first generate a number then checking it to db if exist or not and If not exist then update the row with the random number
Here is the snippet I tried,
with cte as (
select subIdentifier, id from (
SELECT id, LPAD(FLOOR(RAND() * (999999 - 100000) + 100000), 6, 0) AS subIdentifier
FROM property as p1
WHERE "subIdentifier" NOT IN (SELECT uniqueIdentifier FROM property as p2)
) as innerTable group by subIdentifier
)
UPDATE property SET uniqueIdentifier = (
select subIdentifier from cte as c where c.id = property.id
) where property.id != ''
this query returns a set of record for almost all the rows but I have a table of entries of total 20000,
but this query fills up for ~19000 and rest of the rows are null.
here is a current output
[current result picture]
If anyone can help, I am extremely thanks for that.
Thanks
Instead of trying to randomly generate unique numbers that do not exist in the table, I would try the approach of randomly generating numbers using the ID column as a seed; as long as the ID number is unique, the new number will be unique as well. This is not technically fully "random" but it may be sufficient for your needs.
https://www.db-fiddle.com/f/iqMPDK8AmdvAoTbon1Yn6J/1
update Property set
UniqueIdentifier = round(rand(id)*1000000)
where UniqueIdentifier is null
SELECT id, round(rand(id)*1000000) as UniqueIdentifier FROM test;

sql to inset into a table with select query from table having multiple rows

Im trying to figure out the right way to write sql to insert values from a table, where I need to pick Ids from anotehr table.
eg:
INSERT INTO `ts`.`priorityraw`
(`clientid`,`priorityid`,`typeid`)
VALUES
('AA',1,202),
('AA',1,203),
('AA',1,206),
('AA',1,210),
('AA',1,213);
Here clientId and priority is constant. typeid i need to pick from another table.
How can i modify above query to the following format? it gives issue with
as
syntx
INSERT INTO `ts`.`priorityraw`
(`clientid`,`priorityid`,`typeid`)
SELECT 'as01' AS `clientid`, '1' AS `priorityid`,
  id AS `typeid`
FROM
    ts.types
ORDER BY
   id;
INSERT INTO `ts`.`priorityraw`
(`clientid`,`priorityid`,`typeid`)
SELECT 'as01', 1,
id
FROM
ts.types
ORDER BY
id;

Updating column on one table with values from another table

I'm trying to update an empty table notes, with values that come from a column in another table deals:
UPDATE notes
SET notes.content = (
SELECT deals.memo
FROM deals
WHERE deals.id = notes.deal_id
);
this runs with no error but no notes get updated although there are loads of values in memo.
There's no values at all in notes. Can this be the problem?
Perhaps you want to insert rows into notes:
INSERT INTO notes (deal_id, content)
SELECT d.id, d.ememo
FROM deals;
This will add rows into notes with values from the rows in deals.

insert a new record into a mysql table with one of the values incremented by 1

I've got the following table:
productId price
1 price_value1
2 price_value2
3 price_value3
I would like to insert a new product into the table and assign it a new productId. In this case its value equals to 4.
So I want my new table to look like so:
productId price
1 price_value1
2 price_value2
3 price_value3
4 price_value4
So as far as I understand, in order to do that I have to somehow retrieve the max value of productId and insert it using INSERT INTO mytable VALUES (productId + 1, price_value4).
But how do I find out the maximum value of productId?
I tried INSERT INTO mytable VALUES (SELECT MAX(productId) + 1 FROM mytable, price_value4) but it didn't work.
This should Work:
Select the max(productID) and price_value4 as a columns from mytable and insert the result.
INSERT INTO mytable (SELECT MAX(productId) + 1, 'price_value4' FROM mytable);
However, if you are not going to jump some number you can just add an auto increment id key to product_id and then you will have only to insert the price, the product ID will be incremented automatically..
This will do so :
ALTER TABLE mytable
MODIFY COLUMN `productId` INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT;
you can change INT(10) with the INT(5) for example depanding on the size you want to give to your productId column
EDIT :
In return to the OP question in comments why his solution wouldn't work
Some suggetions says you have to make the SELECT statment in insert always between parenthesis
INSERT INTO mytable VALUES ( (SELECT MAX(ID)+1 FROM mytable) , price_value4)
.. In my Case it Return
(1093): You can't specify target table
'mytable' for update in FROM clause
AND HERE IS WHY (Quoting From the documentation)
When selecting from and inserting into the same table, MySQL creates
an internal temporary table to hold the rows from the SELECT and then
inserts those rows into the target table. However, you cannot use
INSERT INTO t ... SELECT ... FROM t when t is a TEMPORARY table,
because TEMPORARY tables cannot be referred to twice in the same
statement
BUT there is away to overcome by using a query instead of the table itself in the FROM, which has the effect of copying the requested table values instead of referencing the one that you are updating..
INSERT INTO mytable VALUES (
(SELECT MAX(ID)+1 FROM (SELECT * FROM mytable ) as mytmp ),
'price_value4');
OR (Quoting From the documentation)
To avoid ambiguous column reference problems when the SELECT and the
INSERT refer to the same table, provide a unique alias for each table
used in the SELECT part, and qualify column names in that part with
the appropriate alias.
INSERT INTO mytable Values ( (SELECT MAX(ID)+1 FROM mytable as mytmp) , 'price_value4')
This is a duplicate question. In order to take advantage of the auto-incrementing capability of the column, do not supply a value for that column when inserting rows.
A simple syntax to create table
CREATE TABLE Product (
productId MEDIUMINT NOT NULL AUTO_INCREMENT,
price INT NOT NULL,
PRIMARY KEY (productid)
);
While inserting supplied default or leave column as blank or supplied value as NULL. Take a look at below code snippet.
INSERT INTO Product (price) VALUES
('10'),('20'),('4'),
('30');
refer this link

MYSQL INSERT - "0 rows inserted". Any suggestions why is that?

INSERT INTO fields (id_region, id_fields_info, subsidy_dka, id_rents_dka, type_uses, id_rented_from, id_categories, id_farmer, id_season)
SELECT
regions.id,
fields_info.id ,
120,
rents_dka.rent_dka,
"собствена",
rented_froms.id,
categories.id_category,
farmers.id,
seasons.id
FROM regions, fields_info, rents_dka, rented_froms, categories, farmers,
seasons
WHERE
region = "Азмък" AND
field_num = 2222 AND
rent_dka = 60 AND
name = "Десислав" AND
id_category = 3 AND
name = "Десислав" AND
season = "2012-2013"
So I have these tables:
regions,
fields_info,
rents_dka,
rented_froms,
categories,
farmers,
seasons
and they are filled with some data.
I've made a form where the user fills the fields with data from these tables, that I've mentioned, and when the submit button is clicked I want to fill table FIELDS in MYSQL with ID's which I get from the data, the user had entered.
to spot the problem, I'd proceed as follow:
execute an insert without cyrillic
remove all the and
make a default insert with default values
if you get "0 rows inserted" it means that the syntax is correct, but the where clause fails to find any matching entry. I suspect the problem is the AND with the cyrillic. Remove the ANDs until the query finds some entries
I got this case when i tried to insert into a table having a column unique and my SQL had the keyword IGNORE in it :
The query:
INSERT IGNORE INTO users SET `user_id` = 7321, `name`= 'test_name', `phone` = '+188888888';
If the query didn't have IGNORE, it would throw an error (because the column user_id was set unique and I already had a row with the same user_id) but due to IGNORE keyword it just ignores the query and hence results in 0 rows inserted.
Note: I know the question asked doesn't has IGNORE key in the query, but might help someone.