dragon8837 2011-07-21 17:10
浏览 90

使用mysql查询多个表

I am trying to query 5 separate tables in my mysql database, the structures are as follows;

item
itemid | item | description | brand | date | time | path |

actor
actorid | name | actorthumb | bio |

brand
brandid | brandname | description | image |

movie
movieid | title | genre | year | moviethumb | synopsis|

request
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate |


Currently I can join 2 of the tables and display the information I need, for example an item, who wears it :

$query = "SELECT * FROM actor, request WHERE actor.actorid = request.actorid and itemid = ".$itemid; 

and in what film, using

$query = "SELECT distinct * FROM movie, request WHERE movie.movieid = request.movieid and itemid = ".$itemid;


However I need to write 1 query that will display the data from all 5 tables and I can display what I need from these.

I think I need to use the JOIN command however I am not sure how to use this?

Please advise.

  • 写回答

1条回答 默认 最新

  • dtxs9017 2011-07-21 17:26
    关注

    This is a very simple query structure to show you how to reference the different tables using their id's. It can be greatly improved upon depending on the results you want

    SELECT 
    i.*, /* This will display all the fields in the item table */
    a.*, /* This will display all the fields in the actor table */
    b.*, /* This will display all the fields in the brand table */
    m.*, /* This will display all the fields in the movie table */
    r.*  /* This will display all the fields in the request table */
    FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r
    /* This joins the request table itemid with the item table itemid */
    WHERE r.itemid = i.itemid 
    /* This joins the request table actorid with the actor table actorid */
    AND r.actorid = a.actorid
    /* This joins the request table brandid with the brand table brandid */
    AND r.brandid = b.brandid
    /* This joins the request table movieid with the movie table movieid */
    AND r.movieid = m.movieid
    

    If you wanted to return a more filtered result set you could add something like this:

    /* Or whatever the id is */
    AND r.requestid = 123 
    

    Example:

    SELECT 
    i.item, i.description, i.brand, /* item table */
    a.name, /* actor table */
    b.brandname, b.description, b.image, /* brand table */
    m.title /* movie table */
    FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r
    /* This joins the request table itemid with the item table itemid */
    WHERE r.itemid = i.itemid 
    /* This joins the request table actorid with the actor table actorid */
    AND r.actorid = a.actorid
    /* This joins the request table brandid with the brand table brandid */
    AND r.brandid = b.brandid
    /* This joins the request table movieid with the movie table movieid */
    AND r.movieid = m.movieid
    AND a.name = 'Rosie Huntington-Whiteley';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c