[英]How to create an SQL query where I can select the max number of one column with an WHERE & AND clause
我正在嘗試創建一個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)
為了澄清這是數據庫和表格:
因此結果將是產品的名稱,數量,價格和表編號WHERE表編號等於X和表X的最大ordernr。
假設有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
如果您使用聚合函數或分組,則在單個查詢中選擇的所有內容都必須是包含在組中的聚合OR。 您可以將部分查詢分解為子查詢,然后再聯接回父查詢嗎?
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
在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
這是測試結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.