1

我有以下查询,可能会合并为一个或两个更大的查询:

SELECT user, password, email FROM users WHERE user = 'njp316' LIMIT 1;

INSERT IGNORE INTO collectors_users (username, password, email, dateadded, subscribed)
VALUES (user, password, email, NOW(), 'Y');

这将拉回多行,因此需要循环:

SELECT barcode FROM collections WHERE user = user;

INSERT IGNORE INTO collections_collections (username, barcode)
VALUES (user, barcode);

显然我对 SQL 不熟悉,所以只是在寻求帮助。谢谢。

4

3 回答 3

3

除非我遗漏了什么……这很简单:

INSERT IGNORE INTO collectors_users
(username, password, email, dateadded, subscribed)
SELECT user, password, email, NOW(), 'Y'
FROM users 
WHERE user = 'njp316' 
LIMIT 1;

INSERT IGNORE INTO collections_collections 
(username, barcode)
SELECT user, barcode 
FROM collections 
WHERE user = user;
于 2013-01-11T19:57:33.823 回答
0

您可以使用子选择。

INSERT INTO collectors_users (username, password, email, dateadded, subscribed)
SELECT user, password, email, NOW(), 'Y' FROM users WHERE user='njp316' LIMIT 1;

它使用 SELECT 作为数据源来注入 INSERT。请注意,列的顺序很重要。

于 2013-01-11T19:57:33.723 回答
0

试试这个:

INSERT IGNORE INTO collectors_users (username, `password`, email, dateadded, subscribed) 
SELECT `user`, `password`, email, NOW(), 'Y' 
FROM users WHERE `user` = 'njp316' LIMIT 1;

INSERT IGNORE INTO collections_collections (username, barcode) 
SELECT `user`,barcode 
FROM collections WHERE `user` = 'njp316';
于 2013-01-11T20:02:27.687 回答