Insert new row with specific values - mysql

In 1 table I need to insert specific rows with specific values, according to other tables values.
UPDATE item_properties2 AS P
INNER JOIN items ON items.id = P.item
INNER JOIN item_groups ON item_groups.idp = items.group_id
SET P.nr = '0'
WHERE P.type = 1140614900 AND items_groups.idp = '1140503406';
This updates the table. But what I need is basically.
(id, type, item, value, shows, nr) VALUES
(78173, 1336983282, 1352706945, 'test Laisvai pastatomas Sharp', 0, 1)
item_properties2.Id - just row id,
item_properties2.type - connect to item_property_groups2.id ,
'item_properties2.item' this connects to item.id ,
item.id have another column which is item.group_id ,
item.group_id is connected to item_groups.id which have another column naked item_groups.idp .
I need to only select items_groups.idp = '1140503406' . And basically i it should add rows in item_properties2 with with specific type value which i entered and only to models with specific item_properties2. item accord to item_groups.idp . I don't know how to do it.

there are some ways to insert a bulk of data from 2 tables into a newer table.
this can be done like this:
INSERT INTO item_properties2 (id, type, item, value, shows, nr)
SELECT Yid, Ytype,Yitem,Yvalue, Yshows, Ynr
FROM items
INNER JOIN item_groups ON item_groups.idp = items.group_id
WHERE items_groups.idp = '1140503406'
Instead of using the VALUES tag, you will now call for a specific select statement. you only need to change the Y values above into your own column names.
Or you could place your input into a stored procedure
the above query will run over your 2 tables and will insert the values that go with the where statement into the 3th table.
CREATE PROCEDURE sInput
( #val1 int, #val2 int, #val3 varchar(10))
AS
INSERT INTO MyTable(val1, val2, val3)
VALUES(#val1, #val2, #val3)
return

Related

Why am i getting "Subquery returns more than 1 row"

Hi I am making a webrowser game and I am trying to get monsters into my data base when I get the error:
Subquery returns more then 1 row
here is my code
INSERT INTO monster_stats(monster_id,stat_id,value)
VALUES
( (SELECT id FROM monsters WHERE name = 'Necroborg!'),
(SELECT id FROM stats WHERE short_name = 'atk'),
2);
any ideas how to fix this problem?
Try use LIMIT 1
INSERT INTO monster_stats(monster_id,stat_id,value) VALUES ((SELECT id FROM monsters WHERE name = 'Necroborg!' LIMIT 1),(SELECT id FROM stats WHERE short_name = 'atk' LIMIT 1),2);
Or you could use Insert from select, with join, if you have relations with 2 tables.
INSERT INTO monster_stats(monster_id,stat_id,value)
(SELECT monsters.id, stats.id, 2 as value FROM monsters
LEFT JOIN stats on monsters.id = stats.monsters_id
WHERE monsters.name = 'Necroborg!'
AND stats.short_name = 'atk'
)
MYSQL insert from select:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
The problem is one or both of the following:
There is more than one monster named 'Necroborg!'.
There is more than on stat named 'atk'.
You need to decide what you want to do. One option (mentioned elsewhere) is to use limit 1 to get only one value from each statement.
A second option is to better specify the where clause so you get only one row from each table.
Another is to insert all combinations. You would do this with insert . . . select and a cross join:
INSERT INTO monster_stats(monster_id, stat_id, value)
SELECT m.id, s.id, 2
FROM (SELECT id FROM monsters WHERE name = 'Necroborg!') m CROSS JOIN
(SELECT id FROM stats WHERE short_name = 'atk');
A third possibility is that there is a field connecting the two tables, such as monster_id. But, based on the names of the tables, I don't think that is true.

Create a table from max and min values in another

I have table with Name|ValueA|ValueB.1|ValueB,2 where Name is not unique.
I want to extract the B.1 and B.2 values for the low and high A vales for each name.
Bob|1|200|205
Bob|2|500|625
Bob|7|450|850
Bob|3|644|125|
Ann|4|120|120
Ann|8|451|191
Ann|9|145|982
I want a new table with unique names with high and low ValueA, ValueB.1, ValueB.2
Bob|1|7|200|450|205|850
Ann|4|9|120|145|120|982
I remember there is some way to use min/max but am not sure how to set up the query to extract the new table.
INSERT newtable (Name, ValueA, ValueB.1, ValueB.2)
SELECT Name,MAX(ValueA),MIN(ValueA),MAX(ValueB.1),MIN(Value B.1),
MAX(ValueB.2),MIN(ValueB.2)
FROM oldtable GROUP BY Name
Should do the trick.
Assuming that values of A are distinct (at least within a name):
INSERT
INTO newtable
SELECT name, mmin.a, mmax.a, mmin.b1, mmin.b2, mmax.b1, mmax.b2
FROM (
SELECT name, MIN(a) mina, MAX(a) maxa
FROM mytable
GROUP BY
name
) md
JOIN mytable mmin
ON (mmin.name, mmin.a) = (md.name, md.mina)
JOIN mytable mmax
ON (mmin.name, mmin.a) = (md.name, md.maxa)

MySQL insert into table with a set of values

I need to insert a table with some values (eg: 'NDA' in this case). This seems to work well if I have just one value to be inserted. I have around a dozen of similar values, is there a was i can tweak this query to insert say { 'NDA', 'SDM', 'APM' } values. Was curious to know if it can be done without a stored procedure or copy pasting the same statements over and changing the values.
INSERT IGNORE INTO customer_feature (customer_id, feature)
SELECT c.id, 'NDA' FROM
customer as c
where c.edition = 'FREE_TRIAL';
Reference: mysql -> insert into tbl (select from another table) and some default values
Is this what you want?
INSERT IGNORE INTO customer_feature(customer_id, feature)
select c.id, f.feature
from customer c cross join
(select 'NDA' as feature union all select 'SDM' union all select 'APM'
) f
where c.edition = 'FREE_TRIAL';

How to insert only edited values in table

I have table "nol_voa" with different values and I am importing xml file with values in that table I want to insert in another table "#tmpRcIzm" the "id" values where the field "C_REF" has changed its value.
This is the code, what I wrote, but there is a mistake, it always adds two more "id" values which have not been changed.
insert into #tmpRcIzm
select distinct
a.id
from
openxml(#hDoc, '/art_komplekts/nol_voa') with #vc xd
join nol_art a on xd.art_cd = a.cd
left join #tmp t on t.cd = xd.art_cd
inner join nol_voa v on xd.id_art = v.id_art
where
xd.C_REF!=v.C_REF
You left join of #tmp, can introduce duplicates, and also the join on nol_art serve no purpose on this SQL. remove those two, and you should elminate your dups.

MySql UNION for UPDATE

Is there a way to update multiple rows with different values for each row using a single SQL query? I have to update one colum in many rows with different data. Using individual update queries for each row seems excessive so if it's possible I would like to consolidate this process into a single SQL statement or at least reduce the number of queries required.
I am using PHP with the Zend framework and MySql.
Create a temporary table and fill it with:
CREATE TEMPORARY TABLE temptable (id INTEGER, VALUE VARCHAR(200))
INSERT
INTO temptable
VALUES
('1', 'val1'),
('2', 'val2'),
('3', 'val3'),
('4', 'val4')
Then issue:
UPDATE
mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id
Don't know about MySQL specifically, but to update multiple rows based on a SELECT, or a UNION of multiple SELECTs, I would do
UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
Update 10/28/2014, converted to work for MySQL:
UPDATE MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
SET MyColumn = T.OtherColumn
I know this works for SQL Server, so it's worth a try in MySQL.
update xtable
set a =
Case
when a = "a"
then z
when a = "b"
then y
End
where ...
You can construct the case statement based on your different rows.