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')

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!