SQL insert query for multiply rows with shared values - sql

I have a simple table Users (name, city, country) and need to add several rows where some of values are the same (city, country). Is there a better way to insert data beside:
insert into Users (name, city, country) values
("John", "Paris", "France"),
("Anna", "Paris", "France"),
("Peter", "Paris", "France"),
("Mary", "Paris", "France")
Thank you

You can use a query like the following:
insert into Users (name, city, country)
select name, city, country
from (select 'John' as name union all
select 'Anna' union all
select 'Peter' union all
select 'Mary') as t1
cross join (select 'Paris' as city, 'France' as country) as t2

You should be able to use variables. So it would look like...
set #city="Paris";
set #country="France";
insert into Users(name, city, country) values
("John", #city, #country)

Although you could simplify this somewhat by using variables in your particular RDBMS syntax, you are stuck with inserting the same values into multiple rows of the table due to a design decision that you made when defining your Users table.
The problem is that your table is not normalized, meaning that the same information is present multiple times. A proper way of fixing this would be defining a table of countries, a table of cities referencing it, and changing Users table to reference Cities.

Your solution is correct normaly but try to replace " by ' else try it:
insert into Users (name, city, country)
select *
from (
select 'John', 'Paris', 'France' union all
select 'Anna', 'Paris', 'France' union all
select 'Peter', 'Paris', 'France' union all
select 'Mary', 'Paris', 'France'
) tmp

Related

Inserting data into table with select

i`m trying to do something.... I have to insert some data in to a table but..... So here is where I end up...
INSERT into HR.my_employees
(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
SELECT
3 AS ID,
'Biri' AS LAST_NAME,
'Ben' AS FIRST_NAME,
substr(FIRST_NAME,1,1)||''||substr(LAST_NAME,1,7) AS userid,
1100 AS salary
FROM dual
UNION
SELECT
4 AS ID,
'Newman' AS LAST_NAME,
'Chad' AS FIRST_NAME,
substr(FIRST_NAME,1,1)||''||substr(LAST_NAME,1,7) AS userid,
750 AS salary
FROM dual;
any suggestion...
You cannot refer to an alias in the SELECT or WHERE clauses of a sub-query where it is defined. Generate the data in a sub-query (or a sub-query factoring clause) and then refer to it in an outer query:
INSERT into HR.my_employees(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
WITH data (id, last_name, first_name, salary) AS (
SELECT 3, 'Biri', 'Ben', 1100 FROM DUAL UNION ALL
SELECT 4, 'Newman', 'Chad', 750 FROM DUAL
)
SELECT id,
last_name,
first_name,
SUBSTR(first_name,1,1) || SUBSTR(last_name,1,7),
salary
FROM data;
or:
INSERT into HR.my_employees(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
SELECT id,
last_name,
first_name,
SUBSTR(first_name,1,1) || SUBSTR(last_name,1,7),
salary
FROM (
SELECT 3 AS id, 'Biri' AS last_name, 'Ben' AS first_name, 1100 AS salary FROM DUAL
UNION ALL
SELECT 4, 'Newman', 'Chad', 750 FROM DUAL
);
fiddle
Why do you want to use select statements? - Based on the values you provide it seems like you want to insert hardcoded values. Or does the dual table contain data you want to insert into HR.my_employees?
If you want to just insert values into the table, but not from an existing one, you can use the following structure:
INSERT INTO table_name (column1, column2, column3, ..., columnXX)
VALUES (value1, value2, value3, ..., valueXX);

SQL(SQL/Oracle) insert value from a select statement

I want to insert (user_id) value from a select statement like below. Can this query will work on both sqlserver and oracle? Kindly advise.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
values (
select max(user_id) from b_user ,
david brown,
david#david.com,
chicago,
il,
usa,
60007)
No, that query won't work in either Oracle or SQL Server. You can do an insert based on a select statement, however. Note that you also have to treat strings as strings and enclose them in quotes.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
select max(user_id) user_id,
'david brown',
'david#david.com',
'chicago',
'il',
'usa',
'60007'
from b_user
If we insert bracket () around the SELECT statement, it will work. I tried for SQL Server and it's working for me. Maybe you can try the following query for the Oracle as well.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
values (
(select max(user_id) from b_user),
'david brown',
'david#david.com',
'123 davids street',
'chicago',
'il',
'usa',
60007
)

How to get unique records from 3 tables

I have 3 tables and I am trying to get unique results from all 3 tables (including other columns from each table).
I have tried union approach but that approach only works when I have single column selected from each table.
As soon as I want another corresponding column value from each table, I don't get unique values for the field I am trying to get.
Sample Database and query available here as well: http://www.sqlfiddle.com/#!18/1b9a6/10
Here is the example tables i have created.
CREATE TABLE TABLEA
(
id int,
city varchar(6)
);
INSERT INTO TABLEA ([id], [city])
VALUES
(1, 'A'),
(2, 'B'),
(3, 'C');
CREATE TABLE TABLEB
(
id int,
city varchar(6)
);
INSERT INTO TABLEB ([id], [city])
VALUES
(1, 'B'),
(2, 'C'),
(3, 'D');
CREATE TABLE TABLEC
(
id int,
city varchar(6)
);
INSERT INTO TABLEC ([id], [city])
VALUES
(1, 'C'),
(2, 'D'),
(2, 'E');
Desired result:
A,B,C,D,E
Unique city from all 3 table combined. By unique, I am referring to DISTINCT city from the combination of all 3 tables. Yes, the id is different for common values between tables but it doesn't matter in my use-case if id is coming from table A, B OR C, as long as I am getting DISTINCT (aka UNIQUE) city across all 3 tables.
I tried this query but no luck (city B is missing in the output):
SELECT city, id
FROM
(SELECT city, id
FROM TABLEA
WHERE city NOT IN (SELECT city FROM TABLEB
UNION
SELECT city FROM TABLEC)
UNION
SELECT city, id
FROM TABLEB
WHERE city NOT IN (SELECT city FROM TABLEA
UNION
SELECT city FROM TABLEC)
UNION
SELECT city, id
FROM TABLEC) AS mytable
try this. As this should give you distinct city with there first appear id:
select distinct min(id) over(partition by city) id, city from (
select * from TABLEA
union all
select * from TABLEB
union all
select * from TABLEC ) uni
You got the right idea, just wrap the UNION results in a subquery/temp table and then apply the DISTINCT
WITH TABLEE AS (
SELECT city, id FROM TABLEA
UNION
SELECT city, id FROM TABLEB
UNION
SELECT city, id FROM TABLEC
)
SELECT DISTINCT city
FROM TABLEE

Get all values in temp table with sql pivot

I have this data at temp table
Country |City
-----------|-------------
Philippines|Mandaluyong
Philippines|Quezon
Philippines|Aurora
America |Example
America |Example2
And I want to display using pivot like this
Philippines|America
-----------|-------------
Aurora |Example
Mandaluyong|Example2
Quezon
But currently, I got this only
Philippines|America
-----------|-------------
Quezon|Example2
Here is my code below. Did I miss something?
DECLARE #TempTable TABLE (Country VARCHAR(MAX), City VARCHAR(MAX))
INSERT INTO #TempTable (Country, City)
SELECT 'Philippines' AS Country, 'Mandaluyong' AS City
UNION ALL
SELECT 'Philippines', 'Quezon'
UNION ALL
SELECT 'Philippines', 'Aurora'
UNION ALL
SELECT 'America', 'Example'
UNION ALL
SELECT 'America', 'Example2'
SELECT * FROM #TempTable
SELECT Philippines, America
FROM
(
SELECT Country, city
FROM #TempTable
)temp
PIVOT
(
MAX (City)
For Country in (Philippines, America)
)piv
The result set was grouping by the Country field, as a result there would be one row per Country, which is what gave the results the way you have.
On including a grouping column (ie numbering the records by country in a row_number) function you would get the results you expect
DECLARE #TempTable TABLE (Country VARCHAR(MAX), City VARCHAR(MAX))
INSERT INTO #TempTable (Country, City)
SELECT 'Philippines' AS Country, 'Mandaluyong' AS City
UNION ALL
SELECT 'Philippines', 'Quezon'
UNION ALL
SELECT 'Philippines', 'Aurora'
UNION ALL
SELECT 'America', 'Example'
UNION ALL
SELECT 'America', 'Example2'
SELECT * FROM #TempTable
SELECT Philippines, America
FROM
(
SELECT row_number() over(partition by Country order by city) as rnk,Country, city
FROM #TempTable
WHERE country in ('Philippines','America')
)temp
PIVOT
(
MAX (City)
For Country in (Philippines, America)
)piv
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=212381456098cd4fd858de3e01257067

Copy one table row to another using SQL

For me, this works:
INSERT INTO table_A (name, age, city) SELECT name, age, city FROM
table_B WHERE id=1
But now I want to set value for the column 'status' in table_A (this values is not present in table_B). Every row must have the 'status=1'. How can I do it?
I tried to do this, but didn't work:
INSERT INTO table_A (status, name, age, city) VALUES (1, SELECT name,
age, city FROM table_B WHERE id=1)
SELECT 1, name, age, city FROM table_B ...
and it has nothing to do with PDO