简体   繁体   中英

MySQL Insert into table with values from Inner Join

I'm trying to insert data into a inventory table, pulling in a UserID from a separate Users table to populate one of the fields.;

 Inventory:
 ProductID | PurchasedByUser | OtherAttributes

and a table of Users;

 Users:
 DBID | UserActive | UserName
   1  |     1      |  mathew

So far my SQL looks like:

 INSERT INTO Inventory (ProductID, PurchasedByUser, OtherAttributes)
 SELECT 23, U.DBID, 'Yellow'
 FROM Inventory U INNER JOIN Users ud 
 ON U.DBID = ud.DBID AND ud.UserActive = 1 AND UserName = 'mathew'

Edit: I want to be able to make a single SQL INSERT request to add ProductID (INT), PurchasedByUser (Users.DBID), OtherAttributes (String) to the Inventory table without exposing the DBID and passing only the UserName field.

In the Query U.DBID is wrong because U is alias of Inventory so please change and put ud.DBID and your query will be resolve.

INSERT INTO Inventory (ProductID, PurchasedByUser, OtherAttributes)
SELECT 23, ud.DBID, Yellow
FROM Inventory U INNER JOIN Users ud 
ON U.PurchasedByUser = ud.DBID AND ud.UserActive = 1 AND ud.UserName = 'mathew'

CHANGE U.DBID TO ud.DBID

INSERT INTO Inventory (ProductID, PurchasedByUser, OtherAttributes)
SELECT 23, ud.DBID, 'Yellow'
FROM Inventory U INNER JOIN Users ud 
ON U.PurchasedByUser = ud.DBID AND ud.UserActive = 1 AND ud.UserName = 'mathew';

In the SQL query you framed, you are referring to DBID of U which is the Inventory table. From the structure you shared, I think you want the following query:

 INSERT INTO Inventory (ProductID, PurchasedByUser, OtherAttributes)
 SELECT 23, ud.DBID, Yellow
 FROM Inventory U INNER JOIN Users ud 
 ON U.PurchasedByUser= ud.DBID AND ud.UserActive = 1 AND UserName = 'mathew'

I'm still confused with what are trying to achieve here, you are fetching values from the Inventory and inserting it back again.. Not sure about that.. Just trying to help you figure out the query!

You can accomplish that in a two-step process. First get the id of the user:

SELECT DBID
FROM Users
WHERE UserActive=1 AND UserName='mathew';

Now that you have the user's id you can insert it into the Inventory table with a second query:

INSERT INTO Inventory (ProductID,PurchasedByUser,OtherAttributes )
VALUES(23,<THE-USER'S-ID>, 'Yellow');

I assume you know how to do this in PHP because you're not asking for PHP code

I believe 23 & Yellow are records And you need a quote for mathew .

  INSERT INTO Inventory (ProductID, PurchasedByUser, OtherAttributes)
  SELECT 23, U.DBID, Yellow
  FROM Inventory U INNER JOIN Users ud 
  ON U.DBID = ud.DBID AND ud.UserActive = 1 AND UserName = mathew

To This

 INSERT INTO Inventory (ProductID, PurchasedByUser, OtherAttributes)
 SELECT ProductID, PurchasedByUser , OtherAttributes
 FROM Inventory U INNER JOIN Users ud 
 ON U.DBID = ud.DBID AND ud.UserActive = 1 AND UserName = 'mathew'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM