Mysql "IF" in query - mysql

I have few tables
table201202
table201203
table201204
table201205
...
tableversion
In tableversion I have columns:
Version_ID Admin_ID MONTH YEAR
I need to get something like this
Select *
FROM tableversion
LEFT JOIN table(tableversion.month tableversion.year) on
table(tableversion.month tableversion.year).Version_ID=tableversion.version_id
But in tableversion I've got months without leading 0 so i must check if there is 5 and then change it to 05, if variable is 12 then nothing. How can I do it?
Ok, so now i have something like this
SELECT *, CONCAT('table',IF(tableversion.month < 10, CONCAT('0', tableversion.month ), tableversion.month ),year) as year
FROM tableversion
LEFT JOIN year ON tableversion.version_id=year.version_id WHERE ADMIN_ID=11
#1146 - Table 'database.year' doesn't exist
But it does not work

As seen here, use LPAD:
SELECT LPAD(month,2,'0') ...

Try this out in the area where you're concerned about comparing the months
IF(tableversion.month < 10, CONCAT('0', tableversion.month), tableversion.month)

I think an IF statement would likely perform poorly, but I have no data to back that claim.
You can try this alternative to an IF statement to achieve the same:
SELECT *, LPAD(month, 2, '0') as month2 ....

Related

Search data on basis of month and date Mysql

select * from table where 1 and DATE_FORMAT(dateColumn,"%d-%m") BETWEEN "01-09" and "30-09"
This query is returning all the data. I just want the data between these dates, without taking into account the year.
In the table dateColumn is yyyy-mm-dd.
You need to do the comparison as MM-DD, not DD-MM:
select *
from table
where DATE_FORMAT(dateColumn, '%m-%d') BETWEEN '09-01' and '09-30'
Or, for this case, you could just do:
where month(dateColumn) = 9
Try this:
SELECT *
FROM table
WHERE MONTH(dateColumn) = 9
You can check if the month is September
select * from table where DATE_FORMAT(dateColumn,"%m")
= '09
If you want rows from your table where the month of your date is 9 and the day of month is between 1 and 30, you can actually do this quite easy and more explicit:
SELECT
*
FROM
table t
WHERE
MONTH(t.date) = 9 and
DAY(t.date) BETWEEN 1 and 30
It's also easier to understand.

php sql query select between month and year

I want select SQL query in where condition between month and year. in my database have field name months and years for store produce input of month and year. now I want search produce input in December,2015 to august 2016
This is my sql
SELECT * FROM tbl_produce p WHERE p.status =0 AND ((p.months >='12'
AND p.years>='2015') AND (p.months <='8' AND p.years<='2016)) ;
but result return NULL, I known my query not correct but how to correct it ?
Why not design your date column as date ?
If you want to keep your current design, try this:
SELECT *
FROM tbl_produce
WHERE status = 0
AND concat(years, if(months > 9, months, concat('0', months)))
BETWEEN '201512' AND '201608'
OR
SELECT *
FROM tbl_produce
WHERE status = 0
AND (
(years = 2015 AND months = 12)
OR
(years = 2016 AND months < 9)
)
Your SQL query is looking for result where p.months is equal to both 12 and 8, and where p.years is equal to both 2015 and 2016.
To achieve the result you are looking for, replace the AND between the two sets of parenthesis with OR, to look like so:
SELECT * FROM tbl_produce p WHERE p.status =0 AND ((p.months >='12' AND p.years = '2015') OR (p.months <='8' AND p.years = '2016));
If I had access to your table structure I would run the test myself to confirm, but I do believe this to be the correct syntax for the result you are looking for.

MySql Select IF determine what condition should be implemented

I have two condition for my query, if count(a.id) is more than 1, than the where condition is DAYS LIKE '%2%' or else DAYS LIKE '%6%'. But I have no idea how to perform this. I have tried that query bellow, but it wrongs.
Can anyone help me? thanks.
Ps. Sorry for my English.
SET #time = '20:59:59';
SELECT *
FROM `msshift` a
WHERE IF(
a.`TIMEFROM` < a.`TIMETO`,
CAST(#time AS TIME) BETWEEN a.`TIMEFROM` AND a.`TIMETO`,
(CAST(#time AS TIME) BETWEEN CAST('00:00:00' AS TIME) AND a.`TIMETO`) OR
(CAST(#time AS TIME) BETWEEN a.`TIMEFROM` AND CAST('24:00:00' AS TIME))
) = 1 AND PLANT = '1011' AND IF(COUNT(a.ID) > 1, a.DAYS LIKE '%2%', a.DAYS LIKE '%6%')
You can't use aggregate functions in a where clause. This is what the 'having' clause is designed for.
I can't tell what the specific query would be without seeing the structure and knowing what result your aiming for but here's an example.
table: foo
id: INT
foreign_id: INT
name: CHAR
SELECT count(*) as grouped_count from foo
GROUP BY foreign_id
HAVING grouped_count > 5
This will return every group of records, belonging to the foreign table, that has more than 5 records in its group.
Hope this helps.

mysql select with priority of 3 filelds in a query

my table and fields are like these:
i must find $sy<year<$ey then it must filter only values by $sm<month<$em at last it must find $sd<day<$ed
i need to find records between dates for example like 2010/10/25 , 2010/10/10
at first i tried :
SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;
but it cant find records for dates like e like 2010/10/25 , 2010/10/28
than i tried
SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
this isnt usful in my case!
i need some thing like priority select first select all between years than month and than day!!
other way is convert mysql records to timestamp by year and month and day and compare it by input date but UNIX_TIMESTAMP('year-month-day 00:00:00') dont worked correct for me.
i used it like :
SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453
If convert to timestamp didn't work for you what about use date_format to convert:
SELECT *
FROM `work_result`
WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') >
DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')

A specific value(only one) to be first in a query result. Rest order by time stamp

I have a search result query for posts. And I want posts written by myself(userId = 27) to be first in query result and rest ordered by time stamp. Can anyone give me query for that in mysql?
select *
from posts
order by
if (userid=27, -1, any_timestamp_include_zero);
include your full table schema help much better
How about something like:
select * from post
order by
case
when userid = 25 then '0001-01-01 00:00:00'
else my_timestamp
end
(formatting the '0001-01-01' part appropriately for MySql)
Something simple like this:
SELECT * FROM POST WHERE userId = 25
UNION
SELECT * FROM POST WHERE userId <> 25 ORDER BY TIMESTAMP_FIELD
Could work for your need?