create table t1 ( id int(11) unsigned not null primary key auto_increment, partner_id varchar(35) not null, t1_status_id int(10) unsigned ); insert into t1 values ("1", "partner1", "10"), ("2", "partner2", "10"), ("3", "partner3", "10"), ("4", "partner4", "10"); create table t2 ( id int(11) unsigned not null default '0', t1_line_id int(11) unsigned not null default '0', article_id varchar(20), sequence int(11) not null default '0', primary key (id,t1_line_id) ); insert into t2 values ("1", "1", "sup", "0"), ("2", "1", "sup", "1"), ("2", "2", "sup", "2"), ("2", "3", "sup", "3"), ("2", "4", "imp", "4"), ("3", "1", "sup", "0"), ("4", "1", "sup", "0"); create table t3 ( id int(11) not null default '0', preceeding_id int(11) not null default '0', primary key (id,preceeding_id) ); create table t4 ( user_id varchar(50) not null, article_id varchar(20) not null, primary key (user_id,article_id) ); insert into t4 values("nicke", "imp"); prepare stmt from 'select distinct t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where exists ( select * from t2 as pl_inner where pl_inner.id = t1.id and pl_inner.sequence <= ( select min(sequence) from t2 pl_seqnr where pl_seqnr.id = t1.id ) and exists ( select * from t4 where t4.article_id = pl_inner.article_id and t4.user_id = ? ) ) and t1.id = ? group by t1.id having count(pp.id) = 0'; set @user_id = 'nicke'; set @id = '2'; execute stmt using @user_id, @id; EXISTS -> IN: prepare stmt from 'select distinct t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.sequence <= ( select min(sequence) from t2 pl_seqnr where pl_seqnr.id = t1.id ) and pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = ? ) ) and t1.id = ? group by t1.id having count(pp.id) = 0'; Rewritten with 1. inner subquery removed prepare stmt from 'select distinct t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = ? ) ) and t1.id = ? group by t1.id having count(pp.id) = 0'; ===> Query fails Rewritten with 1. inner subquery and distinct removed prepare stmt from 'select t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = ? ) ) and t1.id = ? group by t1.id having count(pp.id) = 0'; ===> Query fails Rewritten with 1. inner subquery, distinct and group by/having removed prepare stmt from 'select t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = ? ) ) and t1.id = ?'; ===> Query fails Rewritten with 1. inner subquery, distinct and group by/having removed, 1. parameter replaced with literal value prepare stmt from 'select t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = \'nicke\' ) ) and t1.id = ?'; set @id = '2'; execute stmt using @id; ===> Query fails Rewritten with 1. inner subquery, distinct and group by/having removed, 1. parameter replaced with literal value, 2. left join removed prepare stmt from 'select t1.partner_id from t1 left join t3 on t1.id = t3.id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = \'nicke\' ) ) and t1.id = ?'; set @id = '2'; execute stmt using @id; ===> Query fails Rewritten with 1. inner subquery, distinct and group by/having removed, 1. parameter replaced with literal value, both left joins removed prepare stmt from 'select t1.partner_id from t1 where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = \'nicke\' ) ) and t1.id = ?'; set @id = '2'; execute stmt using @id; ===> Query fails Rewritten with 1. inner subquery, distinct and group by/having removed, both parameters replaced with literal value, both left joins removed prepare stmt from 'select t1.partner_id from t1 where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = \'nicke\' ) ) ; execute stmt; ===> Query fails Rewritten with 1. inner subquery, distinct and group by/having removed, both parameters replaced with literal value, both left joins removed, 2. part of where clause of outer query removed. prepare stmt from 'select t1.partner_id from t1 where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.article_id in ( select t4.article_id from t4 where t4.user_id = \'nicke\' ) )'; execute stmt; ===> Query fails Rewritten with 2. inner subquery removed prepare stmt from 'select distinct t1.partner_id from t1 left join t3 on t1.id = t3.id left join t1 pp on pp.id = t3.preceeding_id where t1.id in ( select pl_inner.id from t2 as pl_inner where pl_inner.sequence <= ( select min(sequence) from t2 pl_seqnr where pl_seqnr.id = t1.id ) ) and t1.id = ? group by t1.id having count(pp.id) = 0'; set @user_id = 'nicke'; set @id = '2'; execute stmt using @user_id; ==> Query succeeds