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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。