Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 显示最新记录并联接多个表_Mysql_Sql - Fatal编程技术网

Mysql 显示最新记录并联接多个表

Mysql 显示最新记录并联接多个表,mysql,sql,Mysql,Sql,如何连接多个表并显示每个用户销售的商品,以及如何显示销售商品的最新记录 我需要这样的输出 销售人: “jon”项目“1”本“书”,价格为“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 -----------------------

如何连接多个表并显示每个用户销售的商品,以及如何显示销售商品的最新记录 我需要这样的输出

销售人: “jon”项目“1”本“书”,价格为“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    |
    -------------------------

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

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
还有一件事。如果字段名是一致的,那么这是一种更好的做法

欢迎来到SO。请参阅: