My SQL query is giving error while trying to execute query:
Error:
Query input must contain at least one table or query
INSERT into Posts (PostText,TimePosted, TID)
VALUES ('My Post','2013-04-11 13:50:18',
(SELECT MAX(TID) FROM Threads AS TID))
Combine the literal fields with the select & move the alias;
INSERT into Posts (PostText,TimePosted, TID)
SELECT 'My Post','2013-04-11 13:50:18', MAX(TID) AS TID FROM Threads
You have not specified table name in your query.
There are 3 fields in your table and you are sending 3 values (Last value by select query).
But you have not written table name for main query in which data to insert.
Related
I have a table SIGHTINGS(NAME, PERSON, LOCATION, SIGHTED), and I'm trying to insert a new row into that table with the following query:
INSERT INTO SIGHTINGS (NAME, PERSON, LOCATION, SIGHTED)
VALUES ('Douglas dustymaiden', 'Person B', 'Double Mountain', '2005-11-28');
But it's returning this error:
[2017-12-04 17:08:18] [1] [SQLITE_ERROR] SQL error or missing database (sub-select returns 6 columns - expected 1)
I've looked up the correct syntax for sqlite inserts here, and from what I can tell, the insert is written correctly. Can someone tell me why it's throwing this error instead of doing the insert? I'm using DataGrip 2017 if that helps identify any issues.
EDIT:
Here's the trigger I added to the database. The insert works without the trigger.
CREATE TRIGGER SightingLocationError
BEFORE INSERT ON SIGHTINGS
FOR EACH ROW
WHEN NEW.LOCATION NOT IN FEATURES
BEGIN
SELECT RAISE(ABORT, 'Error: Insert into the SIGHTINGS table references location that is not found in the database.');
END;
WHEN NEW.LOCATION NOT IN FEATURES
The FEATURES table has six columns, so the database does not know how it should search for the location value.
Use an explicit subquery to return the column you want to use for this:
WHEN NEW.LOCATION NOT IN (SELECT xxx FROM FEATURES)
I'm using Sqlite and I found out that INSERT does not return the just inserted record with the automatically created primary key.
To solve this issue I found out I can use SELECT last_insert_rowid(). This returns the value that I need.
I need this value to update a field in the record of my database.
Is there any way I could form a SELECT query that has the result of that query built in?
UPDATE events SET url='query executed' WHERE eventId = <value of above query should come here>
If SELECT last_insert_rowid() returns the value that you need then put it there as a subquery. Subquery must be in (your_subquery) brackets.
This seems like a simple request. But my query in not working and I'm finding conflicting answers on the internet. Is it possible to have UPDATE and INSERT using a Stored Procedure joining 2 tables in MySql?
I have an Asp.net Webforms website. It has 2 tables Individual and Address. Individual table contains data on an individual, i.e. Phone Number, Fax, Email ect.
The address table has all the address information for an individual. They each table has a column of Individual ID which auto increments. (Note: the individualID in Address table is not a primary key, but individualID in the individual table is a primary key.
Anyway, I have a FormView in Asp.net that with a SELECT statement that joins those 2 tables and display the data fine. But updating new information to both tables keeps failing.
My most recent error is : Duplicate entry '0' for key 'PRIMARY'
Is there a way to write an UPDATE statement that joins 2 Tables?? This has to exist right?
It is possible to update multiple tables with a single query -
UPDATE table1
INNER JOIN table2
ON table1.id = table2.table1_id
SET table1.col1 = 'some value', table2.col1 = 'Another value'
WHERE <some where clause>;
Maybe i should do this in C# but i have more then one row with linkId X. I would like to remove it but i am unsure how. In code i could just use a foreach from 0 to n and remove any found rows with a greater (or !=) id but thats in code. Is there a less difficult way of doing it using sqlite?
Assuming the table's name is tableName and there is a primary key field named id, the following sql would do it. I think the following SQL query is general enough and should be able to be executed under any database engine.
delete from tableName
where id not in (
select min(id) from tableName
group by linkId
)
I want to insert a record into a sqlite table if its actually not inserted.
Let's say it has three fields pk, name, address
I want to INSERT new record with name if that name not added preveously.
Can we do with this in a single Query. Seems like its slightly different from SQL Queries sometimes.
Yes, you can do that with a single query.
INSERT ON CONFLICT IGNORE should help you: http://www.sqlite.org/lang_conflict.html
Put a unique key on the name, this will create a conflict when you try inserting a record if the name already exists.
The default is ABORT, so without the IGNORE, the statement will return an error. If you don't want that, use IGNORE.
If you can't make use of a UNIQUE INDEX in combination with INSERT INTO or INSERT OR IGNORE INTO, you could write a query like this;
INSERT INTO table (column)
SELECT value
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE column = value)