我对SQL
并不擅长,但我尽我所能,因为我所拥有的一点点知识......
我在朋友的帮助下创建了一个单独的SQL
字符串,该朋友使用来自第一个表的初始数据的相对表从表中收集数据,SQL
就像这样:
SELECT id, username, auth_assignment.created_at
FROM `user` JOIN `auth_assignment`
ON (user.id = auth_assignment.user_id)
JOIN `auth_item`
ON (auth_item.name = auth_assignment.item_name)
WHERE auth_item.name = 'Admin'
要查看的初始数据是Admin
所以一切都在那边工作,但我尝试使用SQL
函数模拟这个Yii2
..到目前为止,我已经制作了这段代码
$query = new Query;
$command = $query->select('id, username')
->innerJoin('auth_assignment', ['user.id'=>'auth_assignment.user_id'])
->innerJoin('auth_item', ['auth_item.name'=>'auth_assignment.item_name'])
->where('auth_item.name = :name', [':name'=>$name])->createCommand();
var_dump($command->query());
这会返回SQLSTATE
错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN' at line 1
The SQL being executed was: SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN `auth_item` ON `auth_item`.`name`='auth_assignment.item_name' WHERE auth_item.name = 'Admin'
我检查了方法$command->sql;
以了解SQL
是如何生成的...但由于我对SQL
缺乏了解,我真的不知道如何修复它对yii2
api文档缺乏了解
SQL
生成如下:
SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`=:qp1 INNER JOIN `auth_item` ON `auth_item`.`name`=:qp2 WHERE auth_item.name = :name
我感谢任何帮助
答案 0 :(得分:2)
尝试此查询
$query = (new yii\db\Query())
->select('id, username, auth_assignment.created_at')
->from('user')
->innerJoin('auth_assignment','user.id=auth_assignment.user_id')
->innerJoin('auth_item','auth_item.name = auth_assignment.item_name')
->where([
'auth_item.name' => 'Admin'
])->all();