I have a first table articles where some data are missing.
In the table articles_tmp I have a part of the missing data.
So I want to UPATE articles.path WHERE path IS NULL with the data in articles_tmp.path WHERE path IS NOT NULL.
With SQLite, this doesn't works :
UPDATE articles
SET path = (
SELECT at.path
FROM articles_tmp AS at, articles AS a
WHERE at.article_id = a.id AND a.path IS NULL AND NOT at.path IS NULL
)
WHERE path IS NULL
With MS Access, this works fine :
UPDATE articles
INNER JOIN articles_tmp
ON articles.id = articles_tmp.id
SET articles.path = articles_tmp.path
WHERE articles.path IS NULL AND articles_tmp.path IS NOT NULL
Can someone help ?
Since the SQL Server syntax I provided did not work, here is the solution I came up with. Just a slight modification of your original query, but with my "correlation" suggestion added. Here is an SQL fiddle for it: http://sqlfiddle.com/#!7/34589/7
Here is the code:
UPDATE articles
SET path = (SELECT path FROM articles_tmp AS t WHERE t.id = articles.id)
WHERE path IS NULL;
That works (very correlated, probably too much) :
UPDATE articles
SET path = (
SELECT at.path
FROM articles_tmp AS at
WHERE at.article_id=articles.id
)
WHERE EXISTS (
SELECT *
FROM articles_tmp AS at
WHERE at.article_id=articles.id AND articles.path IS NULL AND NOT at.path=''
)
Related
I have the following query that is supposed to delete from a table with a circular FK requirement;
DELETE FROM question
WHERE defaultGotoQuestionId IN (
SELECT id from question WHERE facilityId IN (
SELECT id FROM facility WHERE customerId NOT IN (1,76)
)
);
But I get the following error;
Table is specified twice, both as a target for 'DELETE' and as a separate source for data
I understand the error message - that I can't DELETE from a TABLE that I am specifying in the WHERE - I just can't seem to work out how to solve it, for myself.
I saw a few examples of using a join - but perhaps I am having an off day - because I have been iterating through copy/paste examples - but still can't manage it.
It works as a SELECT : In that the result gives me the records I want to delete;
SELECT id FROM question
WHERE defaultGotoQuestionId IN (
SELECT id from question WHERE facilityId IN (
SELECT id FROM facility WHERE customerId NOT IN (1,76)
)
);
Thanks for any help!
What version of MariaDB are you using?
DELETE :: Same Source and Target Table
Until MariaDB 10.3.1, deleting from a table with the same source and target was not possible. From MariaDB 10.3.1, this is now possible.
See dbfiddle.
In older versions of MariaDB (or in MySQL, for example), one option you can use is:
DELETE
`question`
FROM
`question`
INNER JOIN (
SELECT
`id`
FROM
`question`
WHERE
`defaultGotoQuestionId` IN (
SELECT
`id`
FROM
`question`
WHERE
`facilityId` IN (
SELECT
`id`
FROM
`facility`
WHERE
`customerId` NOT IN (1, 5)
)
)
) `der` ON
`question`.`id` = `der`.`id`;
See dbfiddle.
According to all examples my query should be executing. I am trying to update new column on my table with the last 4 digits of the phone number like so :
UPDATE users
SET users.phone_last_4 = t.lastFour
FROM
(
select substr( phone_number, -4) as lastFour from users
) t;
Also tried this:
UPDATE users
SET users.phone_last_4 = t.lastFour
FROM
(
select substr( phone_number, -4) as lastFour from users
) AS t;
Both fail with same error :
near ".": syntax error: UPDATE users
SET users.
What could I possibly do wrong here?
SQLite does not support joins for the UPDATE statement and also this syntax containing FROM .
In your case I can't see why you need it.
Just do:
UPDATE users
SET phone_last_4 = substr(phone_number, -4)
I have the Following table Structure. (I am new to SQL Lite)
create table Relations
(
Code int,
ParentCode int ,
fname text
)
GO
insert into Relations values(1,null,'A');
insert into Relations values(2,null,'B');
insert into Relations values(3,2,'C');
insert into Relations values(4,3,'D');
I want to get the initial parent of Code =4 :
i.e. values 2 null B
I am not able to figure out how to write a recursive query in sqllite.
Thanks in Advance..
Was a version issue..
This query was not working & was getting a syntax Error.
I upgraded From 3.7.17 To 3.8.7.4 version & it worked..
WITH RECURSIVE
works(Code,Parent) AS (
Select Code,ParentCode from Relations a where a.Code=4
UNION
SELECT Relations.Code, Relations.ParentCode FROM Relations , works
WHERE Relations.Code=works.Parent
)
SELECT * FROM works where Parent is null
---- Done with Symfony2.3.1 and Doctrine2 ----
Sorry, i hope i was not too stupid to find a suitable solution for my problem. I try to build a Query for hours.
SELECT * FROM product
WHERE product_id in
(
SELECT product_id from (
SELECT count(*) as result_amount, product_id FROM product_x_attribut
JOIN productattribut on productattribut_id = productattribut.id
WHERE (
productkey = "price" and
stringType = "premium"
) or (
productkey = "size" and
stringType = "S"
)
GROUP BY product_id
HAVING result_amount = 2
) as temp
)
GROUP BY product_id
ORDER BY p0_.name ASC
This is the SQL which works fine in phpmyAdmin.
This can be seen like
Select * from abc where abc.x in ( Select * from ( select * from ) as abcd )
So there is one core query, i call it subSubQuery, the second query around the core will be called subQuery and the outer Query is just the outer Query, no a Subquery.
I could build the subSubQuery with Doctrine2.
But i cannot built the subQuery like this
Select product_id from ( $subSubQuery->getQuery()->getDQL() )
I want to do the subQuery like this
$subQuery = $repositoryProduct->createQueryBuilder('product');
$subQuery->add('select', 'product_id');
$subQuery->add('from',$subSubQuery->getDQL() );
// However to set an alias is a miracle for me, this didnt work off course
$subQuery->add('as','tmp' );
This is the subQuery.
I also cannot build the outer Query
Select * from abc where abc.x in ( $subQuery->getQuery()->getDQL() )
I want to do this like this
$query->where(
$query->expr()->in('product.id', $subQuery->getDQL() )
);
But i try to build this with Doctrine2 like this:
I am so down, i tried ->getSQL(), ->getDQL(), i tried as much as i was able to detect as a suitable tiny step to a solution for this problem and i has tried as much keyword in google as my finger were able to write... I hope someone could help me to find a solution...
Thanks a lot to each helpful advise.
I know that statements like this work:
$qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL()));
Your question is kind of hard to follow. Consider using pastebin to show all your mappings as they currently exist. And then maybe presenting a simplieid query?
My $qbGameId query was built with:
$qbGameId = $em->createQueryBuilder();
$qbGameId->addSelect('distinct gameGameId.id');
$qbGameId->from('ZaysoCoreBundle:Event','gameGameId');
// Assorted joins and where conditions
I have a stored procedure which inserts records into a table using values from my table variable. (The table variable is sent to SQL from ASP.NET) It works perfect and looks like this...
CREATE PROCEDURE sp_SaveResponses
(
#TableVariable SaveResponsesTableType READONLY
)
AS
BEGIN
INSERT INTO tbl_Responses
(
assessmentid, questionid, answerid
)
SELECT assessmentid, questionid, answerid
FROM #TableVariable
END
The above inserts one record into tbl_Responses for every row in #TableVariable.
The Problem
Instead of INSERT, I would like to perform an UPDATE, but I can't get the syntax right.
Thanks for any help...!
UPDATE
With some helpful hints, I was able to resolve this below...
You could try this (I haven't tested it) -
CREATE PROCEDURE sp_SaveResponses
(
#TableVariable SaveResponsesTableType READONLY
)
AS
BEGIN
UPDATE tbl_Responses set questionid = #TableVariable.questionid
FROM #TableVariable
WHERE #TableVariable.assessmentid = tbl_Response.assessmentid
END
Depending on what the join is between the table variable and the table that needs to be updated:
CREATE PROCEDURE sp_SaveResponses
(
#TableVariable SaveResponsesTableType READONLY
)
AS
BEGIN
UPDATE
tbl_Responses
SET
questionid = #TableVariable.questionid
FROM
#TableVariable T1
JOIN tbl_Responses T2 ON T1.assessmentid = T2.assessmentID
END
Thanks to #ipr1010 and #cob666 whose answers led me in the right direction... Here is the solution.
UPDATE tbl_Responses SET answerid = T1.answerid
FROM #TableVariable T1
WHERE tbl_Responses.assessmentid = T1.assessmentid AND tbl_Responses.questionid = T1.questionid
Naming #TableVariable T1 resolved the "must declare scalar variable..." issue.
I also needed to update my WHERE clause or all values were updated with the first value in #TableVariable.
I wish I could vote you guys up but apparently my street cred is too weak!