简体   繁体   English

select 从特定日期开始的所有销售? mysql,将日期时间转换为日期

[英]select all sales from specific day ? mysql , convert datetime to date

how to convert the DateTime column to date?如何将 DateTime 列转换为日期?

I need something like the select * from sales where date = = "2022-09-18";我需要类似select * from sales where date = = "2022-09-18";

I need to get all sales from a specific day我需要获得特定日期的所有销售额

I tried this How can I convert datetime to date, truncating the times, leaving me the dates?我试过这个如何将日期时间转换为日期,截断时间,给我留下日期? and many more solutions but nothing is working.还有更多解决方案,但没有任何效果。

i tried this SELECT DATE( datatime ) AS date_of_booking FROM sales; ";我试过这个SELECT DATE( ) AS date_of_booking FROM sales; "; ) AS date_of_booking FROM sales; "; but it gave me this ) AS date_of_booking FROM sales; ";但它给了我这个在此处输入图像描述

I tried also this but it gave me error this我也试过这个,但它给了我这个错误

this is a table named Sales这是一个名为Sales的表

在此处输入图像描述

if there is anybody that can help me understand how do I get this thing to work please i really appreciate that.如果有人可以帮助我了解如何让这件事发挥作用,我真的很感激。 a lot .很多 。

code for generating the database if you want如果需要,用于生成数据库的代码

DROP DATABASE IF EXISTS ice_cream_store;
CREATE DATABASE IF NOT EXISTS ice_cream_store;
USE ice_cream_store; 

CREATE TABLE `Tastes` (
    `tid` INTEGER NOT NULL AUTO_INCREMENT primary key,
    `name`  VARCHAR(20) NOT NULL,
    UNIQUE (name)
);
CREATE TABLE `Toppings` ( 
    `topid` INTEGER NOT NULL AUTO_INCREMENT primary key,
    `name`  VARCHAR(20) NOT NULL,
    UNIQUE (name)
);
CREATE TABLE `Receptacles` ( 
    `rid` INTEGER NOT NULL AUTO_INCREMENT primary key,
    `name`  VARCHAR(20) NOT NULL,
    `price` int NOT NULL,
    UNIQUE (name)
);

CREATE TABLE `Sales` ( 
    `sid` INTEGER NOT NULL AUTO_INCREMENT primary key,
    `rid` integer not null,
    foreign key (`rid`) references Receptacles(`rid`),
    `datetime` datetime not null, 
    `completed` bool not null,
    `paid` bool not null,
`total_price` INTEGER NOT NULL
);

CREATE TABLE `Tastes_Sales` ( 
    `sid` integer not null,
    foreign key (`sid`) references Sales(`sid`),
    `tid` integer not null,
    foreign key (`tid`) references Tastes(`tid`),
    PRIMARY KEY (`sid` , `tid`),
    `quantity` integer not null
);

CREATE TABLE `Toppings_Sales` ( 
    `sid` integer not null,
    foreign key (`sid`) references Sales(`sid`),
    `topid` integer not null,
    foreign key (`topid`) references Toppings(`topid`),
    PRIMARY KEY (`sid` , `topid`)
);

SELECT 
       DATE(`datatime`)
         AS  date_of_booking
FROM sales;


select * from tastes;
select * from Receptacles;
select * from Toppings;
select * from sales;
select * from Toppings_Sales;
select * from Tastes_Sales;

Use the extract function使用提取物 function

Example:例子:

SELECT EXTRACT(MONTH FROM "2017-06-15 09:34:21");

outputs 6输出 6

From there you just do:从那里你只需:

select * from sales where EXTRACT(DAY FROM date) = 18;

Now you just add AND operators to do the same for month and year现在您只需添加 AND 运算符即可对月份和年份执行相同的操作

I think this is a simple enough solution.我认为这是一个足够简单的解决方案。 Hope this works for you.希望这对你有用。

SELECT * from sales WHERE DATE( datetime ) = '2022-09-15'; SELECT * 来自销售 WHERE DATE( datetime ) = '2022-09-15'; this is working for me @RiggsFolly thank you very much!!这对我有用@RiggsFolly 非常感谢!

在此处输入图像描述

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

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