dsilhx5830 2017-03-13 19:19
浏览 32
已采纳

查询从相关表中获取数据

I have 3 tables.

galery_images:

  • id | int (primari key)
  • image | varchar (the link on the server)
  • tartalom | longtext
  • galery | int
  • order | int

galery:

  • id | int (primari key)
  • name | varchar
  • tartalom | longtext

1-n relation with the images

galery_category:

  • id | int (primari key)
  • name | varchar

n-1 relation with the galeries

I'm asking for an image, then I would like to fetch all the relevant data in a single query. I need all the columns, aka, 1 image, 1 galery, and all the categories.

I'm using codeigniter, so I would also like to know what I'm facing while retrieving the categories.

  • 写回答

1条回答 默认 最新

  • dongzhong9055 2017-03-13 19:38
    关注

    Assuming galery is linking column between galery_images and galery table and name is linking column between galery and galery_category table, you can use the following query.

    SELECT gi.image, gi.tartalom, gi.galery, gi.order, g.tartalom, gc.name
    FROM galery_images gi JOIN galery g ON gi.galery = g.name
    JOIN galery_category gc ON g.name = gc.name
    WHERE gi.image = ? //Other conditions
    

    Please note that it will return multiple records for same image because of 1-many relationship.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大