TOFREE007 2016-05-11 01:44 采纳率: 0%
浏览 2462

MYSQL中多对多关系的数据查询处理,请大神指点

用户表
create table user(
uid int primary key auto_increment,
uname varchar(30),
uaddress varchar(15))
订单表
create table orders(
oid int primary key auto_increment,
uid int not null,
address varchar(70));
商品表
create table product(
pid int primary key auto_increment,
pname varchar(20) not null,
pcount int not null,
price float not null)
中间表
create table op(
opid int primary key auto_increment,
oid int ,
pid int ,
opcount int not null,
opdelete set('n','y')
,constraint fk_oid foreign key (oid) references orders(oid) on update cascade
,constraint fk_pid foreign key (pid) references product(pid) on update cascade
);
我使用的是hibernate中HQL多表查询
select o.oid, p.pname, op.opcount, p.pid from Orders as o, OP as op, Product as p, User as u where o.uid=u.uid and op.oid=o.oid and op.pid=p.pid and u.uid=001;
关系是user表跟orders表是一对多,orders表跟product表是多对多。
我数据库中user uid=001,oid=1000,它对应的pid有多个1000001,1000002,1000003
我现在做出来的查询结果是
每一次for循环,oid记录只输出一个pid的数据;我现在有3个pid,需要循环3次才可以输出来。
但是我实际需要的效果是 同一个oid一条记录就可以将它所对应的所有商品都遍历出来。

  • 写回答

1条回答

  • 普通网友 2016-10-07 17:23
    关注

    create table orders(
    oid int primary key auto_increment,
    uid int not null,
    address varchar(70));
    商品表
    create table product(
    pid int primary key auto_increment,
    pname varchar(20) not null,
    pcount int not null,
    price float not null)
    中间表
    create table op(
    opid int primary key auto_increment,
    oid int ,
    pid int ,
    opcount int not null,
    opdelete set('n','y')

    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊