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
Related
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
table 1 - id, name, address, item, date, price, ...
table 2 - id, name, item
table 2 cannot have duplicate rows (while table 1 may have some).
by using 'insert where not exists' does its included rows which inserted at that insert itself ?
insert into table2 (id,name,item)
select id, name, item
from table1
where not exists
(select 1 from table2 where table2.id=table1.id and table2.name=table1.name and table2.item=table1.item)
No. This can be handled using select distinct:
insert into table2 (id, name, item)
select distinct id, name, item
from table1
where not exists
(select 1 from table2 where table2.id=table1.id and table2.name=table1.name and table2.item=table1.item) ;
This question already has answers here:
SQL query to find duplicate rows, in any table
(4 answers)
Closed 4 years ago.
SELECT FirstName, LastName, MobileNo, COUNT(1) as CNT
FROM CUSTOMER
GROUP BY FirstName, LastName, MobileNo;
Something like this will produce duplicates of the table Customer based on FirstName, LastName and MobileNo. However, I would like to produce a list of duplicates based on ALL columns (which are unknown). How would I accomplish this?
You could use checksum(*)
sqlfiddle.com/#!18/0a33d/4
Ex.
CREATE TABLE TEST_DATA
( Field1 VARCHAR(10),
Field2 VARCHAR(10)
);
INSERT INTO TEST_DATA VALUES ('1','1');
INSERT INTO TEST_DATA VALUES ('1','1');
INSERT INTO TEST_DATA VALUES ('2','2');
INSERT INTO TEST_DATA VALUES ('2','2');
INSERT INTO TEST_DATA VALUES ('2','2');
INSERT INTO TEST_DATA VALUES ('3','3');
SELECT TD1_CS.*
FROM (SELECT TD1.*,
CHECKSUM(*) CS1
FROM TEST_DATA TD1
) TD1_CS
INNER
JOIN (SELECT CHECKSUM(*) CS2
FROM TEST_DATA TD2
GROUP
BY CHECKSUM(*)
HAVING COUNT(*) > 1
) TD2_CS
ON TD1_CS.CS1 = TD2_CS.CS2
SELECT FirstName, LastName, MobileNo, COUNT(*)
FROM CUSTOMER
GROUP BY FirstName, LastName, MobileNo
HAVING COUNT(*) > 1
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
I'd like to join 2 tables together. TableB has 15 distinct values that I want to go into the TableA (the one I'm inserting into). However, I also want to insert individual values for TableA. For example, I want to insert the 15 individual values from TableB into TableA, but I also want to be able to insert another field ('region') in TableA
--so far I have this
insert into TableA ((id)
select distinct(id) from TableB
group by id), region values('NYC')
I'm not sure how to insert the region in there...the above fails. I need to hardcore the regions in there because they are not in the other table.
You probably want something like this:
insert into TableA (id, region)
select distinct id, 'NYC'
from TableB
INSERT INTO TableA (ID, REGION)
SELECT ID, 'NYC'
FROM TableB
GROUP BY ID
That's it.
The DISTINCT is redundant.
Try something like this:
insert into TableA (id, region)
select distinct id, "NYC"
from TableB
group by id