From 39c94104a5b0b572c3d76790d3508f12517a1c3a Mon Sep 17 00:00:00 2001 From: llseng <1300904522@qq.com> Date: Fri, 8 Nov 2019 10:23:11 +0800 Subject: [PATCH 1/2] Get method uses sql clause --- MysqliDb.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index e264f7a9..cc149baf 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -723,7 +723,9 @@ public function get($tableName, $numRows = null, $columns = '*') $column = is_array($columns) ? implode(', ', $columns) : $columns; - if (strpos($tableName, '.') === false) { + if (is_object($tableName)) { + $this->_tableName = $this->_buildPair("", $tableName); + }else if (strpos($tableName, '.') === false) { $this->_tableName = self::$prefix . $tableName; } else { $this->_tableName = $tableName; From fad1909db29f8917686fb06e0505270989cb8765 Mon Sep 17 00:00:00 2001 From: llseng <1300904522@qq.com> Date: Fri, 8 Nov 2019 10:54:27 +0800 Subject: [PATCH 2/2] subquery in selects --- readme.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/readme.md b/readme.md index 3a400ed1..be26a96e 100644 --- a/readme.md +++ b/readme.md @@ -681,6 +681,13 @@ $ids->get ("products", null, "userId"); $db->where ("id", $ids, 'in'); $res = $db->get ("users"); // Gives SELECT * FROM users WHERE id IN (SELECT userId FROM products WHERE qty > 2) + +$db::$prefix = "t_"; +$subQuery = $db->subQuery('u'); +$subQuery->orderBy('loginCount', 'desc')->get( "users, (select @i:=0) r", 10, ['(@i:=@i+1) ranking','id', 'login', 'firstName']); + +$data = $db->where( 'login', 'user3')->get( $subQuery, null, ['ranking']); +// Gives SELECT ranking FROM (SELECT (@i:=@i+1) ranking, id, login, firstName FROM t_users, (select @i:=0) r ORDER BY loginCount DESC LIMIT 10) u WHERE login = 'user3' ``` Subquery in inserts: