I want to achieve the following in single SQL Query.
I am inserting values into table "A" for the columns "A1,A2".
I am passing the value for "A1" column directly.
For the column "A2", I need to select the "B2" column from "B" table and pass the value to "A2".
Incase if the "B" table does not contains the value for the condition which i am checking in my query, then i need to insert new row in "B" table and pass the B2 value to "A2".
My above query works well if the B table has value and condition matches.
I am not able to make it when the B table is not having/matching the value. Need help to insert new row and pass the newly inserted row to table A.
INSERT INTO A(A1,A2) VALUES('A1 Value',(select B2 from B where action = 'test'))
You can't do this in one query, since you can only insert into one table at a time. So there's no way to insert a new row into B in the same query if the row is missing.
Assuming action has a unique index, you can do it in 2 queries like this:
INSERT IGNORE INTO B (action, B2) VALUES ('test', 'default value');
INSERT INTO A (A1, A2)
SELECT 'A1 Value', B2
FROM B
WHERE action = 'test';
INSERT IGNORE will do nothing if action = 'test' already exists.
Related
I have a request to remove some rows from a source.
This source contains 3 columns : Id, Type, Value, and containes some data like :
Id Type Value
1 Master This is the first value
1 Second This is a new value
1 Third This is not a mandatory value
2 Master Another one
2 Third And again
3 Second A new hope
3 Third A third
4 Second A single value
...
The rule to keep row is :
If single row for one Id, get the existing value
Else :
If multiple rows for same Id and 'Master' exists, get the 'Master' value
If multiple rows for same Id and 'Master' not exists and 'Second' exists, get the 'Second' value
If multiple rows for same Id and 'Master' not exist and 'Second' not exists and 'Third' exists, get the 'Third' value.
In my sample so, I would like to extract only :
Id Type Value
1 Master This is the first value
2 Master Another one
3 Second A new hope
4 Second A single value
I try split into 3 different sources and join or lookup, but not found any parameter to discard the duplicate row.
How I can do that ?
Thanks in advance,
BR
Xavier
Try put them through a sorter to order by ID then Type and finally through an aggregater grouping by ID asc, desc depending on your requirements (lucky master comes before second which comes before third alphabetically)
Please find the query which can be converted in to informatica map.
create table test1
( id integer,
type varchar2(100),
value varchar2(1000)
);
insert into test1 values (1,'Master','This is the first value');
insert into test1 values (1,'Second','This is a new value');
insert into test1 values (1,'Third','This is not a mandatory value');
insert into test1 values (2,'Master','This is the first value');
insert into test1 values (2,'Third','This is not a mandatory value');
insert into test1 values (3,'Second','This is the first value');
insert into test1 values (3,'Third','This is not a mandatory value');
insert into test1 values (4,'Second','mandatory value');
commit;
select * from test1;
From the below query "agg" part can be done in aggregator and decode function within aggregator transformation.
Then use joiner to join agg part and actual table
use filter to filter the required rows
with agg as
(select max(case when type='Master' then 10
when type='Second' then 9
when type='Third' then 8
else 7 end) ms,
id
from test1
group by id
)
select a.ms,b.* from agg a, test1 b
where a.id=b.id
and case when a.ms=10 then 'Master'
when a.ms=9 then 'Second'
when a.ms=8 then 'Third'
else 'noval2'
end =b.type;
I have 2 SQL tables, table1 and table2.
table1 has two columns and I want to insert values to these columns.
one of the columns should get a static value and the other column should get a value that is a result of a query from table2.
If I wanted to insert the static data separately I would do:
INSERT INTO table1(login_id)
VALUES ('1234');
and if I wanted to insert the dynamic value separately I would do:
INSERT INTO table1(user_uuid)
SELECT users_uuid FROM table2 where first_name like 'ortal';
How can insert both values to table1 in one action?
If I try the first query I get:
11:20:45 INSERT INTO table1(login_id ,user_uuid) VALUES ('1234') Error Code: 1136. Column count doesn't match value count at row 1 0.000 sec
INSERT INTO `users`.`table1` (`login_id`) VALUES ('1234');
ERROR 1364: 1364: Field 'user_uuid' doesn't have a default value
You can add the constants to your select list and treat them a columns:
INSERT INTO table1(user_uuid, login_id)
SELECT users_uuid, '1234' FROM table2 WHERE first_name LIKE 'ortal';
I want some field datas to get inserted from a table(called profile) to another table(services) along with those field datas in the table(to which the values want to be inserted,i.e. services).But I am failing. I am new to MySQL and database queries i need ur help.
$qur = mysql_query("INSERT INTO services(FirstName,LastName,DOB,Mobile,email,CountryCode,Address,State,City,Country,PinCode,altmobnumber,PanCard,AdharCard,ServiceOffering,Fee,FeeDuration,FeeExtraHour,negotiable) select profile.FirstName,profile.LastName,profile.DOB,profile.Mobile,profile.email,profile.CountryCode,profile.Address,profile.State,profile.City,profile.Country,profile.PinCode from profile where id = '$id'")or die(mysql_error());
You need to specify value for every column in your VALUES list. If you do not have any value from the SELECT statement, either use a NULL value for respective field or simply remove it from the VALUES list.
e.g. if you want to insert three values in tbl1 but tbl2 contains only 2 fields, use a NULL value for the third field
INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, NULL FROM tbl2
or if you have a hard coded value,
INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, some_value FROM tbl2
or simply use only two fields
INSERT INTO tbl1 (a, b) SELECT tbl2.a, tbl2.b FROM tbl2
Now, the VALUES part of your query has 19 fields but your SELECT statement provides only 11 fields. Either add NULL for the last 8 fields for successful execution or remove the last 8 fields from VALUES list
I have a string column which has duplicate values, for example
row 1 column "desc" has value -> "stack-overflow stack-overflow"
I want to update the column to "stack-overflow"
any idea how to build a query for that?
duplicate record must be symmetric and should be repeated twice only then following will do the trick
select
left
(
'stack-overflow stack-overflow',
length('stack-overflow stack-overflow')/2
)
When there is request from user i get parameter x,y,flag and sent. I insert it into table2.
$sql1 = "INSERT INTO table2
(id,sent,pcount,ncount,flag)
VALUES
('".$_POST['id']."','".$_POST['sent']."',' $x ','$y','$flag')";
When data is inserted, below that I am showing chart, which takes inserted values x and y to show the chart.
So how on button click event I can get the values inserted in last insert operation??
Is it possible that I can create view with last row x and y values from table table2.
Table table2 has auto increment column.
CREATE VIEW view_name AS
SELECT xcolumn,ycolumn
FROM table2 WHERE id=SELECT MAX(id)
Assuming you have an incrementing column called id.
If you want to get data from the last insert, use last_insert_id(). This will give you the last inserted id for your connection.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id