我正在尝试创建一个SQL查询。我被困住了,因为我无法获得想要的结果。我正在使用phpMyAdmin尝试这些查询,
我尝试使用GROUP BY和WHERE子句,但无法使其正常工作。
SELECT products.product_name,
orderitem.quantity,
products.product_price
FROM orderitem
INNER JOIN producten ON orderitem.FKproductID = products.productID
INNER JOIN orders ON orderitem.FKproductID = orders.orderID
INNER JOIN tables ON orders.FKtableID = tables.tableID
WHERE tables.tablelnr = '1'
和
SELECT products.product_name,
orderitem.quantity,
products.product_price,
tables.tablenr,
MAX(orders.ordernr)
FROM orderitem
INNER JOIN products ON orderitem.FKproductID = producten.productID
INNER JOIN orders ON orderitem.FKorderID = orders.orderID
INNER JOIN tables ON orders.FKorderID = tables.tablelID
GROUP BY tables.tablenr
和
SELECT products.product_name,
orderitem.quantity,
products.product_price
FROM orderitem
INNER JOIN producten ON orderitem.FKproductID = products.
INNER JOIN orders ON orderitem.FKproductID = orders.
INNER JOIN tables ON orders.FKtableID = tables.tableID
WHERE tables.tablelnr = '1'
AND orders.ordernr = MAX(order.ordernr)
为澄清起见,这是数据库和表:
因此结果将是产品名称,数量,价格和表号,其中表号等于X和表X的最大订购量。
假设有4个订单和4个订单号。三个订单属于表1,一个订单属于表2。那么我想获得最高的订单号(3),其中表号= 1。
所以基本上我想要一列的最大数目,也要等于的数目。
如果对我的解释有任何疑问,请询问。这很难解释。
我的表格数据(phpmyadmin SQL导出):
-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 25, 2019 at 08:04 PM
-- Server version: 10.1.38-MariaDB
-- PHP Version: 7.3.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `overflow`
--
-- --------------------------------------------------------
--
-- Table structure for table `orderitem`
--
CREATE TABLE `orderitem` (
`orderItemID` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
`FKorderID` int(11) NOT NULL,
`FKproductID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `orderitem`
--
INSERT INTO `orderitem` (`orderItemID`, `quantity`, `FKorderID`, `FKproductID`) VALUES
(1, 1, 1, 1),
(2, 1, 1, 3),
(3, 1, 2, 2),
(4, 5, 3, 4),
(5, 2, 4, 1),
(6, 1, 4, 2);
-- --------------------------------------------------------
--
-- Table structure for table `orders`
--
CREATE TABLE `orders` (
`orderID` int(11) NOT NULL,
`ordernr` int(11) NOT NULL,
`FKtableID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `orders`
--
INSERT INTO `orders` (`orderID`, `ordernr`, `FKtableID`) VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 4, 2);
-- --------------------------------------------------------
--
-- Table structure for table `products`
--
CREATE TABLE `products` (
`productID` int(11) NOT NULL,
`product_name` text NOT NULL,
`product_price` decimal(4,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `products`
--
INSERT INTO `products` (`productID`, `product_name`, `product_price`) VALUES
(1, 'cola', '2.50'),
(2, 'pepsi', '2.50'),
(3, 'egg', '6.00'),
(4, 'cake', '10.00');
-- --------------------------------------------------------
--
-- Table structure for table `tables`
--
CREATE TABLE `tables` (
`tableID` int(11) NOT NULL,
`tablenr` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tables`
--
INSERT INTO `tables` (`tableID`, `tablenr`) VALUES
(1, 1),
(2, 2);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `orderitem`
--
ALTER TABLE `orderitem`
ADD PRIMARY KEY (`orderItemID`),
ADD KEY `FKorderID` (`FKorderID`),
ADD KEY `FKproductID` (`FKproductID`);
--
-- Indexes for table `orders`
--
ALTER TABLE `orders`
ADD PRIMARY KEY (`orderID`),
ADD KEY `FKtableID` (`FKtableID`);
--
-- Indexes for table `products`
--
ALTER TABLE `products`
ADD PRIMARY KEY (`productID`);
--
-- Indexes for table `tables`
--
ALTER TABLE `tables`
ADD PRIMARY KEY (`tableID`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `orderitem`
--
ALTER TABLE `orderitem`
MODIFY `orderItemID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
--
-- AUTO_INCREMENT for table `orders`
--
ALTER TABLE `orders`
MODIFY `orderID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products`
MODIFY `productID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `tables`
--
ALTER TABLE `tables`
MODIFY `tableID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `orderitem`
--
ALTER TABLE `orderitem`
ADD CONSTRAINT `orderitem_ibfk_1` FOREIGN KEY (`FKorderID`) REFERENCES `orders` (`orderID`),
ADD CONSTRAINT `orderitem_ibfk_2` FOREIGN KEY (`FKproductID`) REFERENCES `products` (`productID`);
--
-- Constraints for table `orders`
--
ALTER TABLE `orders`
ADD CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`FKtableID`) REFERENCES `panbake`.`tafels` (`tafelID`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
所需的数据将是这样的,但是在ordernr处,只有最高的数据而不是所有的ordernr: https://snag.gy/kUln45.jpg
答案 0 :(得分:0)
如果您使用汇总函数或分组依据,则在单个查询中选择的所有内容都必须是分组依据或包含在分组依据中。您可以将查询的一部分分解为子查询,然后再加入父查询吗?
SELECT products.product_name, orderitem.quantity, products.product_price
FROM Products
INNER JOIN orderitem on orderitem.FK = products.id
INNER JOIN(
SELECT MAX(orders.ordernr), tables.tablelnr, orders.ID
FROM Orders
INNER JOIN tables on orders.FK = tables.tableID
WHERE tables.tablelnr = '1'
GROUP BY Tables.tablelnr, orders.id) as tbl
ON products.fk = tbl.id
答案 1 :(得分:0)
在MySQL中,您需要:
set @tablenr = 1; --You are able to change the variable value here
select Product_name, quantity, product_price, ordernr, tablenr
from (
select Product_name, quantity, product_price, ordernr, tablenr, rank() over (order by ordernr desc) rnk
FROM orderitem oi
INNER JOIN products p ON oi.FKproductID = p.productID
INNER JOIN orders o ON oi.FKorderID = o.orderID
INNER JOIN tables t ON o.FKtableID = t.tableID
where tablenr = @tablenr ) a
where rnk = 1
这是测试结果: