简体   繁体   中英

How to create an SQL query where I can select the max number of one column with an WHERE & AND clause

I am trying to create an SQL Query. I am stuck since I can't get the result that I want. I am using phpMyAdmin to try out these query's,

What I have tried:

I have tried to use the GROUP BY and WHERE clause, but I can't get it to work.

Query's I have tried:

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'

and

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

and

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)

For clarification this is the database and tables:

在此输入图像描述 The tables content:

Result I tried to have

So the result would be the name of the product, the quantity, the price and the table number WHERE tablenumber is equal to X AND the maximum ordernr of the table X.

Let's say there are 4 orders with 4 order numbers. Three orders belong to table 1 and one order belongs to table 2. Then I would like to get the highest order number(3) and where table number = 1.

So basically I want the highest number of a column, and also where something is equal to.

If there are any questions regarding my explanation, please ask. This is quite difficult to explain.

My table data(phpmyadmin SQL export):

-- 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 */;

The desired data would be like this, but then at ordernr only the highest instead of all ordernr: https://snag.gy/kUln45.jpg

If you use an aggregate function or group-by, everything being selected in a single query will need to be an aggregate OR included in the group by. Can you break out part of your query into a sub-query and then join back to the parent?

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

In MySQL, you need to:

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

Here is the test result:

DB<>Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM