简体   繁体   English

在mysql和其他条件中选择最后插入的行

[英]selecting last inserted row in mysql and other conditions

im having the table name fiscalyear 我的表名为fiscalyear


These are the columns 这些是列

  • id(auto increment) id(自动增量)
  • begin (financial year begin date) 开始(财政年度开始日期)
  • end (financial year end date) 结束(财政年度结束日期)
  • active (either 0(open) or 1(closed)) 有效(0(开放)或1(关闭))

  • begin column consists dates like 2012-01-01 begin列包含2012-01-01之类的日期

  • end column consists dates like 2013-12-31 结束列包含2013-12-31之类的日期

i want to get the financial year begin and end date 我想获得财政年度的开始和结束日期

These are the conditions 这些是条件

  1. which is open ie 0, 这是开放的,即0,
  2. also it should be the last inserted. 它也应该是最后插入的。

Here is the query i tried please update my query according to my conditions 这是我试过的查询请根据我的条件更新我的查询

SELECT CONCAT(BEGIN,'/',END) AS YEAR FROM fiscalyear WHERE active='0' ORDER BY `id` DESC LIMIT 1

THE ABOVE QUERY SELECT DATE IN THIS FORMAT: 2012-01-01/2013-12-31 BUT INSTEAD OF THIS I WANT TO PRINT THE SELETED DATE IN THIS FORMAT 2012-13.. 以上格式的上一个查询选择日期:2012-01-01 / 2013-12-31但是我想要打印2012-13版本格式的预订日期。

If you want the last row, you can order the table in DESC order by id which is auto increment. 如果你想在最后一排,你可以订购通过DESC顺序表id是自动递增。

SELECT `BEGIN`,`END` 
FROM `fiscalyear` 
WHERE `active`='0' 
ORDER BY `id` DESC
LIMIT 1

It should be noted that BEGIN and END are reserved words in MySQL, and should be represented as fields using "`" 应该注意的是,BEGIN和END是MySQL中的保留字,应该用“`”表示为字段

As for printing the date, I recommend PHP class DateTime 至于打印日期,我推荐PHP类DateTime

Order it by the id primary key, and use a LIMIT 1 , so you only get the last record. 通过id主键对其进行LIMIT 1 ,并使用LIMIT 1 ,因此您只能获得最后一条记录。 Use the built in DATE_FORMAT to format your date. 使用内置的DATE_FORMAT格式化日期。

SELECT
    CONCAT(DATE_FORMAT(`BEGIN`, '%Y'), '-',
    DATE_FORMAT(`END`, '%Y')) AS mydates
FROM
    `fiscalyear`
WHERE
    `active` = 0
ORDER BY
    `id` DESC
LIMIT 1

Edit: I changed the query to concat the years so it will output 2012-2013 . 编辑:我将查询更改为连续年份,以便输出2012-2013 The result will be in mydates . 结果将在mydates If you want the end year to be 13 instead of 2013 format, then use %y a lowercase y in the format string. 如果您希望结束年份为13而不是2013格式,则在格式字符串中使用%y小写y

About your second question, you can define Begin and End in your database as DateTime (i remember some data type like this was defined in mySQL) and then you can use this function: 关于你的第二个问题,你可以在数据库中定义Begin和End作为DateTime(我记得有些数据类型是在mySQL中定义的)然后你可以使用这个函数:

/* Analyzes time-date and returns mktime!*/
function analyze_time_date($tdstr)
{
    // tdstr : 2010-08-12 11:41:14
    //         0000000000111111111
    //         0123456789012345678
    $year   = (int)substr($tdstr,0,4);
    $month  = (int)substr($tdstr,5,2);
    $day    = (int)substr($tdstr,8,2);

    $hour   = (int)substr($tdstr,11,2);
    $minute = (int)substr($tdstr,14,2);
    $second = (int)substr($tdstr,17,2);

    return mktime($hour,$minute,$second,$month,$day,$year);
}

/*  */
function format_time_date($tdstr,$format)
{
        return date($format,analyze_time_date($tdstr));
}

when you wanna insert a new row to your database you should use 'date time field name' = NOW() in your SQL query. 当你想在数据库中插入一个新行时,你应该在SQL查询中使用'date time field name' = NOW()

Here is the query what i need 这是我需要的查询

SELECT
CONCAT(DATE_FORMAT(`BEGIN`, '%Y'), '-',
DATE_FORMAT(`END`, '%y')) AS mydates

FROM 0_fiscal_year WHERE closed = 0 ORDER BY id DESC LIMIT 1 FROM 0_fiscal_year WHERE closed = 0 ORDER BY id DESC LIMIT 1

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

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