【MySQL 之子查询】教程文章相关的互联网学习教程文章

mysql – IF中的子查询,作为具有空值的列的值【代码】

鉴于:t1{id,type} t2{type,table1_id}我正在使用这个:SELECT IF(t1.type IS NULL, 'some default', t1.type) as ret from t1我想做这样的事情:SELECT IF(t1.type IS NULL, IF((SELECT t2.type FROM t2 WHERE t2.table1_id=t1.id LIMIT 1) IS NOT NULL,table2.type,'some defaults'),t1.type ) as ret from table1解决方法:这个 – SELECT IF(t1.type IS NULL, IF((SELECT t2.type FROM t2 WHERE t2.table1_id=t1.id LIMIT 1)IS...

mysql子查询奇怪的慢【代码】

我有一个查询从另一个子查询选择中进行选择.虽然两个查询看起来几乎相同,但第二个查询(在此示例中)运行速度要慢得多:SELECTuser.id,user.first_name-- user.*FROM userWHEREuser.id IN (SELECT ref_id FROM education WHERE ref_type='user' AND education.institute_id='58' AND education.institute_type='1');此查询需要1.2s解释此查询结果:id select_type table type possible_keys key key_len ref rows Extra1 ...

如何使用mysql子查询减去库存和销售?【代码】

试图了解有关子查询的更多信息.我正在寻找一种减去和比较两个表的方法. >库存>销售 我的数据记录如下: 库存:mysql> select store_id, product_id, sum(quantity) as inventory from inventories where store_id = 1 group by product_id; +----------+------------+-----------+ | store_id | product_id | inventory | +----------+------------+-----------+ | 1 | 8 | 24 | | 1 | 10 |...

mySQL:子查询到数组?【代码】

我正在研究一个包含子查询的mySQL查询的轻微复杂(至少对我而言)并不诚实.SELECT `products`.`id`, `product`.`price`, ( SELECT `value` FROM (`productValues`) WHERE `productValues`.`product` = 'product.id') as values FROM (`products`) WHERE`product`.`active` = 1目前的结果如下:Array ([0] => Array([id] => 1[active] => 1[price] => 1000[values] => ) )我想要的是values元素也成为一个数组,其中Values表中的所有元...

使用子查询mysql在同一个表中更改auto_increment【代码】

我正在使用mysql.我有一个带auto_increment计数器集的数据库表.现在因为一个要求我需要离开100个ID并从101开始移动所有现有记录,所以当前id 1将变为101,id 2将变为102,依此类推. 我能够将记录移动到101,但问题是如何将auto_increment计数器更改为max(id)1. 这里的主要约束是我需要在单个sql语句中执行它.我无法使用@counter保存该值并在以后使用它. 我尝试使用以下查询ALTER TABLE role AUTO_INCREMENT = (SELECT rd.counter FROM ...

使用SUM,WHERE,GROUP_BY的MySQL子查询【代码】

我是初学者(不是DBA).我的数据的简单版本==>我希望得到的结果:|ball |color|count| |ball |Total Blue|Total Red| ------------------- ---------------------------- |b1 |red | 2 | ====> |b1 | 5 | 2 | |b1 |blue | 3 | |b2 | 3 | 1 | |b1 |blue | 2 | |b2 |red | 1 | |b2 |blue | 3 | 我想把每个球...

mysql – 子查询返回多行..如何处理它?【代码】

SELECT requestID FROM request WHERE userId = (SELECT userIDFROM departmentWHERE desig = 'E'AND dept = (SELECT deptFROM departmentWHERE userId = it18AND desig = 'FM') );解决方法:使用JOIN编写此查询会更清楚:select distinct r.requestID from request rjoin department don d.userId = r.userIDand desig = 'E'join department d2on d2.dept = d.deptand d2.desig = 'FM'and d2.userId = 'it18'或者,你可以简单地用I...

php – 在MySQL中编写子查询是一个好习惯吗?【代码】

我正在编写以下子查询以用于某些项目特定目的:SELECT count(*) from table1 WHERE userB='$p' AND userA IN (SELECT userB FROM table1 WHERE userA='$row[username]')我很好奇这是用PHP做的最好的做法还是我应该采用传统方式首先得到子查询结果然后计算记录?解决方法:I was curious if this was the best practice when doing it in PHP or should I resort to the conventional way of first getting the subquery result and...

mysql – 使用子查询和“HAVING”极慢的SQL查询【代码】

我有这个查询,我想计算“user”中有多少条目在“usernames”中有多个条目SELECT usernames.NAME,COUNT(user.id) FROM user INNER JOIN user_username ON user.id = user_username.user_id INNER JOIN usernames ON user_username.user_username_id = usernames.id WHERE user.datecreated BETWEEN '2016-01-01' AND '2016-09-01'AND user.id IN (SELECT user_idFROM user_usernameGROUP BY user_idHAVING COUNT(*) > 1) GROUP BY u...

在MySQL中使用子查询在同一个表上编写SQL查询【代码】

我有一个表svn1:id | date | startdate 23 2002-12-04 2000-11-1123 2004-08-19 2005-09-1023 2002-09-09 2004-08-23从svn1中选择id,startdate,其中startdate> =(从svn1中选择max(date),其中id = svn1.id); 现在问题是如何让子查询知道id与外部查询中的id匹配.显然id = svn1.id不会工作.谢谢!If you have the time to read more:这真的是一个简化版本,询问我在这里想要做什么.我的实际查询是这样的select id, count(distinct ar...

mysql – 子查询返回超过1行的sql查询【代码】

我有这样的错误可以解释我做错了什么?我只添加这个SQL查询:(? = (select travel_region_id from relationships where travel_id = travels.id))错误ActiveRecord::StatementInvalid (Mysql::Error: Subquery returns more than 1 row: select count(*) from travel_start_days, cars, travelswhere travels.id = travel_start_days.travel_id and travels.id = travel.car_id and travel_start_days.day > adddate(curdate(), in...

mysql – 使用Zend Framework 2 TableGateway加入子查询【代码】

我正在尝试使用Zend Framework 2进行查询,其中我在JOIN语句中有一个SELECT.到目前为止,这是我尝试过的,但是将SELECT对象注入join()的第一个参数似乎不起作用.我采用了这种方法,因为我需要在进行任何分组之前先订购结果.有关如何使其工作的任何想法?public function getSearchKeyword($keyword, $limit) {$select = $this->keywords->getSql()->select();$subquery = $this->pages->getSql()->select();$subWhere = new \Zend\Db\S...

为什么我必须在mysql子查询中将一点(1)转换为unsigned?【代码】

出于某种原因,我必须显式地将BIT(1)MySQL列转换为UNSIGNED以便返回0或1 IF我将该列包含为子查询的一部分但不作为“普通”查询.请允许我详细说明…… 我有两个数据库表:竞赛和投票CONTESTS - id, ..., ... VOTES - ..., over BIT(1), ..., ...如果我运行此查询,我会得到我期望的结果…SELECT votes.over as vote_over FROM votes WHERE votes.contest_id = 38;结果:vote_over: 1 (ok, this makes sense)但是,如果我尝试选择投票...

mysql – 使用ActiveQuery的Yii2,它在同一个Model上使用子查询【代码】

我有一个带有parent_id外键的Comments表,该外键指向自身以启用线程注释.Comments id INT UNSIGNED NOT NULL parent_id INT UNSIGNED NULL comment TEXT NOT NULL created_time DATETIME NOT NULL最初的ActiveQuery就是这样class CommentActiveQuery extends \yii\db\ActiveQuery {public function andWhereIsNotRemoved() {return $this->andWhere(['isRemoved' => Comment::STATUS_IS_NOT_REMOVED]);}public function andWherePar...

mysql – WHERE in(子查询)子句yii【代码】

我有这样的mysql查询(SELECT `notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorNameFROM `notification` INNER JOIN `event` ON event.id = notification.source_id INNER JOIN `user` as vON v.id = notification.user_id AND notification.activity_type = "checkin" w...