显示最新记录并连接多个表

h5qlskok  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(250)

如何连接多个表并显示每个用户销售的商品,显示销售我需要的商品的最新记录输出如下
销售人:“jon”物品“#1”“book”,价格为“1000”
尝试:

SELECT uid , users.name AS uname, transact.transaction_id AS transacted INNER JOIN users on transaction_table.c_id=c_table.c_id

   User table
    --------------------------
    | uid | name |  timezone |
    --------------------------
    |  1  | jon  |  +1 gmt   |
    |  2  | mix  |  +2 gmt   |
    |  3  | vic  |  +1 gmt   |
    --------------------------

    transaction table
    -------------------------------
    | transaction_id | uid | c_id |
    -------------------------------
    | dafsf22sdfssgs | 2   |  1   |
    | 23425asda3afaa | 1   |  1   |
    -------------------------------

    C-table
    ------------------------
    | c_id | c_name | price |
    ------------------------
    |  1   | book   | 1000  |
    |  2   | comic  | 100   |
    |  3   | notes  | 10    |
    -------------------------
xxe27gdn

xxe27gdn1#

如果要按项目名称分组并获取总数

select u.name,count(*) as count, c.c_name, c.price*count(*) as totalPrice from user u
inner join transaction t on u.uid=t.uid
inner join ctable c on c.c_id=t.c_id
group by c.c_name

如果要查询所有事务

select u.name, c.c_name, c.price from user u
inner join transaction t on u.uid=t.uid
inner join ctable c on c.c_id=t.c_id

如果您只想返回上一个事务信息

select u.name, c.c_name, c.price from user u
inner join transaction t on u.uid=t.uid
inner join ctable c on c.c_id=t.c_id
order by t.transaction_id desc limit 1

还有一件事。如果您的字段名是一致的,那么这是一种更好的做法。

相关问题