mySql将两个SELECT查询与JOIN组合在一起

时间:2016-08-11 12:22:05

标签: php mysql

我有两个mySql查询来获取数据库的结果。我想和他们一起加入。

查询1:

expr "4" : '-\{0,1\}[0-9]\{1,\}$'

查询2:

SELECT userEwallets.id as ewalletId, users.id as userId , money_repositories.money as money, a.nestedUserId
FROM userEwallets
JOIN users ON users.id = userEwallets.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR937303656'

然后我的组合命令:

SELECT nested.id as nestedUserId
FROM userEwallets as nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR9122331743'

我收到此错误:

SELECT userEwallets.id as ewalletId, users.id as userId , money_repositories.money as money, a.nestedUserId
FROM (
    SELECT nested.id as nestedUserId
    FROM userEwallets as nested
    JOIN users ON users.id = nested.userId
    JOIN money_repositories ON nested.id = money_repositories.ewalletId
    WHERE ewalletNumber = 'SHIRR912233'
)    as a
JOIN users ON users.id = userEwallets.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR93730'

两个命令都相同,但它们在#1054 - Unknown column 'userEwallets.id' in 'field list' 子句中的ewalletNumber之间存在简单差异

使用数据库结构更新

where表:

money_repositories

CREATE TABLE IF NOT EXISTS `money_repositories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userId` int(11) NOT NULL, `ewalletId` int(11) NOT NULL, `money` int(11) NOT NULL, `createdAt` int(11) NOT NULL, `updatedAt` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=4 ; 表:

userEwallets

CREATE TABLE IF NOT EXISTS `userEwallets` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userId` int(11) NOT NULL, `ewalletNumber` varchar(15) COLLATE utf8_persian_ci NOT NULL, `currencySymbol` varchar(5) COLLATE utf8_persian_ci NOT NULL, `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=7 ; 表:

users

2 个答案:

答案 0 :(得分:1)

在选择组合命令中,

userEwallets.id应为a.id。因为您将子查询作为a

修改后的组合查询;

SELECT a.id                     AS ewalletId, 
       users.id                 AS userId, 
       money_repositories.money AS money, 
       a.nesteduserid 
FROM   (SELECT nested.id AS nestedUserId, 
               nested.id, 
               nested.ewalletnumber 
        FROM   userewallets AS nested 
               JOIN users 
                 ON users.id = nested.userid 
               JOIN money_repositories 
                 ON nested.id = money_repositories.ewalletid 
        WHERE  nested.ewalletnumber = 'SHIRR912233') AS a 
       JOIN users 
         ON users.id = userewallets.userid 
       JOIN money_repositories 
         ON userewallets.id = money_repositories.ewalletid 
WHERE  a.ewalletnumber = 'SHIRR93730' 

修复了camleCase查询:

SELECT a.id                     AS ewalletId, 
       users.id                 AS userId, 
       money_repositories.money AS money, 
       a.nestedUserId 
FROM   (SELECT nested.id AS nestedUserId, 
               nested.id, 
               nested.ewalletNumber 
        FROM   userEwallets AS nested 
               JOIN users                         ON users.id   = nested.userId 
               JOIN money_repositories  ON nested.id = money_repositories.ewalletId 
        WHERE  nested.ewalletNumber = 'SHIRR912233') AS a 

       JOIN users                         ON users.id            = nested.userId 
       JOIN money_repositories  ON userEwallets.id = money_repositories.ewalletId 

WHERE  a.ewalletNumber = 'SHIRR937303' 

答案 1 :(得分:0)

因为您不在查询中包含表userEwallets,除了在子查询之外无法识别的子查询中。尝试

<强>更新

Select 
     users.id as userId, userEwallets.id As ewalletId,money_repositories.money
From
    users
Inner Join 
    userEwallets ON userEwallets.userId= users.id
Inner Join 
    money_repositories ON money_repositories.userId=users.id
And
    money_repositories.ewalletId = userEwallets.id
Where
    userEwallets.ewalletNumber = 'SHIRR93730'