我对数据库的原则查询有疑问,我不确定出了什么问题。这是交易:这是我用来创建数据库的实际 mysql 查询:
create table productos (
codigoProducto int (8) unsigned auto_increment primary key,
nombre varchar(100) not null,
id_categoria smallint(3) unsigned,
descripcion text,
foto varchar(100),
precio smallint (4) unsigned not null,
visitado int (7),
habilitado boolean,
foreign key (id_categoria)
references categorias(id)
on update cascade
on delete restrict
)engine=innoDB;
create table facturas(
id int (8) unsigned auto_increment primary key,
id_comprador int (7) unsigned,
metodo_pago boolean,
forma_entrega boolean,
fecha datetime,
foreign key (id_comprador)
references compradores(id)
on update cascade
on delete cascade
)engine=innoDB;
create table detallefactura(
id int(8) unsigned auto_increment primary key,
id_producto int(8) unsigned,
id_factura int(8) unsigned,
cantidad smallint (4),
foreign key(id_producto)
references productos(codigoProducto)
on update cascade
on delete cascade,
foreign key(id_factura)
references facturas(id)
on update cascade
on delete cascade
)engine=innoDB;
"detelleFactura" 是 'productos' 和 'facturas' 的连接表,以便每张发票有超过 1 个产品。因此,通过以下方式,我尝试将所有购买的产品都放在一张发票中:
static function traerProductosComprados($id){
$query = Doctrine_Query::create()
->select('d.id, p.nombre, p.precio, d.cantidad')
->from('Detallefactura d')
->leftjoin('d.Productos p')
->where('d.id_factura = ?',$id);
return $query->execute()->toArray();
}
如果我回显查询,它会显示以下内容:
SELECT d.id, p.nombre, p.precio, d.cantidad FROM Detallefactura d LEFT JOIN d.Productos p WHERE d.id_factura = ?
当我将此粘贴到 phpmyadmin 时,它会引发以下错误:
1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'WHERE d.id_factura = ? LIMIT 0, 30' 在第 1 行;
我现在没有想法,请告诉我是否还有其他我想念的东西!