How to create two subqueries in MySQL
? effectively and optimized.
The current code throws me the error #1241 - Operando debe tener 1 columna(s)
from the phpMyAdmin .
- The first subquery calculates the total number of available classes.
- The second subquery I want to calculate the average and total votes issued.
My code:
$sql = "SELECT video.title,video.icon,video.duration,video.url,video.author,level.level,
(
SELECT COUNT(*)
FROM 'lessons'
WHERE video.ident_video = lessons.ident_video
),
(
SELECT AVG(vote) AS avg_vote, COUNT(*) votos
FROM 'coments'
WHERE video.ident_video = coments.ident_video
)
FROM 'video'
LEFT JOIN 'level' on video.ident_level = level.ident_level
WHERE video.ident_state=? ORDER BY video.ident_video DESC LIMIT 5";
Error: error # 1241 - Operand must have 1 column (s)
Tables SQL
:
CREATE TABLE 'level' (
'ident_level' smallint(3) unsigned not null auto_increment primary key,
'level' varchar(15) not null
)engine=InnoDB default charset=utf8 collate=utf8_spanish_ci;
CREATE TABLE 'state' (
'ident_state' smallint(3) unsigned not null auto_increment primary key,
'state' varchar(15) not null
)engine=InnoDB default charset=utf8 collate=utf8_spanish_ci;
CREATE TABLE 'video' (
'ident_video' int(11) unsigned not null auto_increment primary key,
'title' varchar(255) not null,
'icon' varchar(100) not null,
'duration' varchar(10) not null,
'url' varchar(100) not null unique,
'author' text not null,
'ident_level' smallint(3) unsigned not null,
'ident_state' smallint(3) unsigned not null,
foreign key('ident_level') references 'level'('ident_level')
on delete cascade on update cascade,
foreign key('ident_state') references 'state'('ident_state')
on delete cascade on update cascade
)engine=InnoDB default charset=utf8 collate=utf8_spanish_ci;
CREATE TABLE 'lessons' (
'ident_lesson' smallint(11) unsigned not null auto_increment primary key,
'page' int(11) unsigned not null,
'title' varchar(100) not null,
'url' varchar(100) not null unique,
'status' smallint(1) not null,
'ident_video' int(11) unsigned not null,
foreign key ('ident_video') references 'video'('ident_video')
on delete cascade on update cascade
)engine=InnoDB default charset=utf8 collate=utf8_spanish_ci;
CREATE TABLE coments(
'ident_coment' smallint(11) unsigned not null auto_increment primary key,
'title' varchar(255) not null,
'coment' text not null,
'vote' float(3) not null,
'ident_video' int(11) unsigned not null,
'ident_user' int(11) unsigned not null,
'status' smallint(1) unsigned not null,
foreign key ('ident_video') references 'video'('ident_video')
on delete cascade on update cascade,
foreign key('ident_user') references 'user'('ident_user')
on delete cascade on update cascade
)engine=InnoDB default charset=utf8 collate=utf8_spanish_ci;