php - 在选择查询中使用选择查询?

标签 php mysql sql

I am trying to use a select query within another select query in the from clause.This is a mysql query.

SELECT invoiceNo, SUM(weight), COUNT(barcode), sum_total
FROM (SELECT * FROM `selected_items` WHERE date LIKE '07-Jan-2016' ORDER BY id DESC)
WHERE date LIKE '07-Jan-2016' GROUP BY invoiceNo;

This is how my database looks like. enter image description here How can I achieve something like this where I have a table which I order by id desc and use another select query on the result of this query.The above query gives me an error #1248 - Every derived table must have its own alias.Can anyone help I am new to programming.

最佳答案

希望对您有所帮助。

SELECT invoiceNo, SUM( weight ) , COUNT( barcode ) , sum_total
FROM (SELECT * FROM  `selected_items` 
WHERE DATE LIKE  '07-Jan-2016'
ORDER BY id DESC
)a
GROUP BY invoiceNo;

关于php - 在选择查询中使用选择查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34670494/

相关文章:

mysql - 将日期转换为周

mysql - SQL查询错误 "DEFAULT NULL"

Mysql ANY NOT EQUAL TO 子查询不工作

mysql - SQL GROUP BY语句只能用number来分组?

javascript - 使用 Javascript 和 PHP 从数据库中删除项目

php cli 没有apache?

mysql - 如何在 MySQL 中返回数据透视表输出?

sql - 如何将动态列添加到现有表

php - Yii:更改用于分页的 Sql 请求

php - 可以通过索引访问 PHP parse_str 数组值吗?