On 6/26/2018 9:15 AM, Csányi Pál wrote:
I have the 'SchoolYearTeachingDays' table with just one column, in
which are dates:

CREATE TABLE SchoolYearTeachingDays (
     aDate DATE PRIMARY KEY
                 UNIQUE
);
I filled it with many dates which are unique. These dates excludes
dates for Sundays and for Saturdays. I have another, the
'TeachingSaturdaysInSchoolYear' table:

CREATE TABLE TeachingSaturdaysInSchoolYear (
     id            INT  PRIMARY KEY
                        UNIQUE,
     aDate      DATE,
     TimetableForTheDay TEXT
);
This table holds just two dates. These two dates are for two
Saturdays. On these two Saturdays we have to teach students. When I do
the following query on this table, I get these two records:

2018-04-14
2018-05-05

I want to INSERT these two dates from the
'TeachingSaturdaysInSchoolYear' table into 'SchoolYearTeachingDays'
table.

I am trying with this query:

INSERT INTO SchoolYearTeachingDays
  SELECT aDate FROM TeachingSaturdaysInSchoolYear
;
but I get this error: Error: UNIQUE constraint failed:
SchoolYearTeachingDays.aDate

This means that, despite your assumption to the contrary, at least one of those 
two dates is already present in SchoolYearTeachingDays

Then I get help and this code:
INSERT INTO SchoolYearTeachingDays
  SELECT aDate FROM TeachingSaturdaysInSchoolYear T WHERE T.aDate NOT
IN (SELECT S.aDate FROM SchoolYearTeachingDays S)

This says "insert all dates that aren't already present", which of course 
avoids UNIQUE constraint.

I wish to know followings.
How many times want to inserts the SELECT query the one of the date
from the TeachingSaturdaysInSchoolYear table into
SchoolYearTeachingDays table?

I don't understand this question.

That is: the how many times wants select statement to insert one
record from first table into second table?

Each row in the resultset of SELECT statement is inserted once, of course.
--
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to