简体   繁体   English

如何创建SQL查询,我可以使用WHERE&AND子句选择一列的最大数量

[英]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. 我正在尝试创建一个SQL查询。 I am stuck since I can't get the result that I want. 我被困住了,因为我无法得到我想要的结果。 I am using phpMyAdmin to try out these query's, 我正在使用phpMyAdmin来试用这些查询,

What I have tried: 我尝试过的:

I have tried to use the GROUP BY and WHERE clause, but I can't get it to work. 我曾尝试使用GROUP BY和WHERE子句,但我无法使其工作。

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. 因此结果将是产品的名称,数量,价格和表编号WHERE表编号等于X和表X的最大ordernr。

Let's say there are 4 orders with 4 order numbers. 假设有4个订单,包含4个订单号。 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. 三个订单属于表1,一个订单属于表2.然后我想得到最高订单号(3)和表号= 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导出):

-- 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 所需的数据将是这样的,但然后在ordernr只有最高而不是所有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. 如果您使用聚合函数或分组,则在单个查询中选择的所有内容都必须是包含在组中的聚合OR。 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: 在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

Here is the test result: 这是测试结果:

DB<>Fiddle DB <>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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