I'm attempting to do an INSERT
into Accounts
based upon a CustNo
value, with the tables connected by AccountId
. For some reason I cannot figure out, the SELECT cust.CustNo
column, based on the Custs
table columns of GivenName
and Surname
, is not grabbing anything despite working by the same WHERE clause when tested on it's own.
INSERT INTO Accounts (Type, Balance, CustNo)
SELECT
'Chequing', 0, cust.CustNo
FROM
Custs cust
INNER JOIN
Accounts ON cust.CustNo = Accounts.CustNo
WHERE
cust.GivenName = 'FirstName' AND cust.Surname = 'LastName';
I can't think of any reason why this is not returning any errors, yet also works and grabs the correct CustNo
as a separate SELECT
with this same WHERE
. Am I missing an essential field somewhere to relate these two tables, or possibly incorrectly joining them?
While I did not quite end up answering my original question, I DID however find another way to accomplish what I was trying to do with a nested SELECT
instead of an INNER JOIN
.
For anyone else that comes by trying to INSERT
data to one table based upon a field (My Name
, Surname
, and CustNo
problem) that is in another related table, try something like this:
INSERT INTO Accounts (Type, Balance, CustNo)
SELECT 'Chequing', 0, (SELECT CustNo FROM Custs)//This is the solution, a nested Select
WHERE GivenName = 'FirtName' AND Surname = 'LastName');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.