i have table like this:
+----+--------------------+-------------------+
| id | name | date_departure |
+----+--------------------+-------------------+
| 1 | karyawan karantina | 2017-02-10 |
| 2 | karyawan berangkat | 2015-11-24 |
| 3 | asdf | 2013-04-18 |
+----+--------------------+-------------------+
i want to show data where date_departure o later than 1 year of departure .
so the expected data like this :
+----+--------------------+-------------------+
| id | nama | tanggal_berangkat |
+----+--------------------+-------------------+
| 2 | karyawan berangkat | 2015-11-24 |
+----+--------------------+-------------------+
please help me... thanks before... sorry for my english. Im indonesian...
Assuming you're after flights within 1 year of current date..
This seems to be a pretty straight forward SQL statement... what's the problem?
Manual with relevant functions
SELECT ID, Name, Date_Departure
FROM tablename
WHERE date_Departure between date_Add(now(),Interval -1 year) and
date_Add(now(),Interval +1 year)
or if you truely mean between now and 1 year from now you don't need the date add on the first segment of the between.
SELECT ID, Name, Date_Departure
FROM tablename
WHERE date_Departure between now and
date_Add(now(),Interval 1 year)
Related
Okay, so I have a table which has 3000 rows. Each row is unique, however some fields are duplicated.
I want to update the rows with fields that are duplicated. I'm having a hard time putting this into words, so I'm sorry if I'm getting it all wrong.
For example, the table looks like this:
+----+--------+--------+-----------+-------+-------+------+--+
| id | name | place | day | time1 | time2 | dupe | |
+----+--------+--------+-----------+-------+-------+------+--+
| 1 | George | Garden | Sunday | 12:00 | 13:00 | 0 | |
| 2 | George | House | Monday | 15:00 | 18:00 | 0 | |
| 3 | David | School | Wednesday | 15:00 | 18:00 | 0 | |
| 4 | Stan | Church | Sunday | 12:00 | 13:00 | 0 | |
+----+--------+--------+-----------+-------+-------+------+--+
I'd like to run a mysql query that checks the table for duplicate names in the name field, and marks the dupe field as 1 if they are a duplicate.
So the dupe fields in row 1 and 2 should be '1' and the rest should be '0'.
Thank you for any help you might be able to provide! I hope I explained it right.
You can use EXISTS in a CASE statement:
select
t.*,
case
when exists (
select 1 from tablename
where id <> t.id and name = t.name
) then 1
else 0
end dupe
from tablename t
You seem to only care about the name field. So:
update t join
(select name
from t
group by name
having count(*) >= 2
) dups
on t.name = dups.name
set dups = 1;
I have a MySQL table like this:
acco_id | room_id | arrival | amount | persons | available
1 | 1 | 2015-19-12 | 3 | 4 | 1
1 | 2 | 2015-19-12 | 1 | 10 | 1
1 | 1 | 2015-26-12 | 4 | 4 | 1
1 | 2 | 2015-26-12 | 2 | 10 | 1
2 | 3 | 2015-19-12 | 2 | 6 | 0
2 | 4 | 2015-19-12 | 1 | 4 | 1
What im trying to achieve is a single query with a result like:
acco_id | max_persons_available
1 | 22
2 | 4
I tried using a GROUP BY accommodation_id using a query like:
SELECT
accommodation_id,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1
GROUP BY
accommodation_id
Only now the result of acco_id uses all arrival dates. When I add arrival to the query no more unique acco_id's.
Does anyone know a good Single SQL which can use the table indexes?
If I'm understanding the question correct (the last part is a bit confusing). You want to have the accomodation id and numbers as you have now but limited to specific arrival dates.
If so the following statement should do exactly that as it is not necessary to put arrival into the select if you "just" use it in the where statement. As else you would need to put it into the group by and thus have non unique accomodation id's.
SELECT
accommodation_id,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1 and arrival >= '2015-12-19' and arrival < '2015-10-26'
GROUP BY
accommodation_id
I guess (reading your question) what you are looking for is this but im not sure as your question is a bit unclear:
SELECT
accommodation_id,
arrival,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1
GROUP BY
accommodation_id, arrival
i want to display a query result like this:
| Who_was_born_this_year | Who_was_born_year_ago |
___________________________________________________
| x | y |
| x | y |
where x will display Name of people who born in this year and y will display Name of people who born a year ago
TABLE PEOPLE STRUCTURE:
ID people | ID Gender | Name | Birthday
_________________________________________
1 | 1 | John | 18-07-2015
2 | 1 | Stu | 12-01-2014
3 | 2 | Leslie | 10-03-2014
All date format is dd-mm-yyyy
How can i achieve this with a query?
Thank you
You can try this:
SELECT
IF (YEAR(CURDATE()) = YEAR(Birthday), Name, '') Who_was_born_this_year,
IF (YEAR(CURDATE())-1 = YEAR(Birthday), Name, '') Who_was_born_year_ago
From people
;
but you will receive something like:
| Who_was_born_this_year | Who_was_born_year_ago |
___________________________________________________
| John | |
| | Stu |
| | Leslie |
and i suppose that it's not exactly what you desire...
But it's the best in this situation, because we don't have key how we should bind data for this year and for last year...
Or:
SELECT
(
SELECT group_concat(Name)
FROM people
WHERE YEAR(CURDATE()) = YEAR(Birthday)
) Who_was_born_this_year,
(
SELECT group_concat(Name)
FROM people
WHERE YEAR(CURDATE())-1 = YEAR(Birthday)
) Who_was_born_year_ago
;
but in this case you will receive something like:
| Who_was_born_this_year | Who_was_born_year_ago |
___________________________________________________
| John | |
| | Stu,Leslie |
and i again suppose that it's not exactly what you desire...
I think that it's not the best idea to represent data in way you wish...
You can use following query this query may help you,
SELECT
(CASE YEAR(Birthday) WHEN YEAR(curdate()) THEN name ELSE NULL END) as Who_was_born_this_year,
(CASE YEAR(Birthday) WHEN YEAR(curdate() - interval 1 year) THEN name ELSE NULL END) as Who_was_born_year_ago
FROM people WHERE YEAR( Birthday )IN (YEAR(curdate() - interval 1 year),YEAR(curdate()));
you can also optimize this query by selecting curent year and previous year firstly
I have payment table info like this
ID Costumer | start_pay | Payment
1 | 2014-01-01 | 1.500
2 | 2013-12-01 | 900
that information they must pay every month, i want calculating it for range between start_pay to CURDATE
if CURDATE is 2014-03-01 (Y-m-d) the result I want like this
ID Costumer | start_pay | Payment | total_to_pay | month_count
1 | 2014-01-01 | 1.500 | 4.500 | 3
2 | 2013-12-01 | 900 | 3.600 | 4
can i do that with mysql query?
Try
SELECT *,TIMESTAMPDIFF(MONTH, DATE_SUB(start_pay,INTERVAL 1 MONTH), CURDATE()) AS
month_count,Payment * month_count AS total_to_pay FROM TABLE
Note that if the difference is less than a month it will output 0
PERIOD_DIFF is basically made for this type of calculation:
SELECT PERIOD_DIFF(DATE_FORMAT(CURDATE(),'%Y%m'), DATE_FORMAT(start_pay, '%Y%m')) from your table;
I need help with a SQL statement. The goal is to count the amount of alarms of each date. My table looks something like this:
|----DATE----|---COUNTER---|---ALARM_ID---|
|2012-01-01 | 30 | 1 |
|2012-01-01 | 20 | 2 |
|2012-01-01 | 10 | 3 |
|2012-01-02 | 5 | 1 |
|2012-01-02 | 25 | 2 |
|2012-01-02 | 12 | 3 |
|2012-01-03 | 33 | 1 |
|2012-01-03 | 43 | 2 |
|2012-01-03 | 11 | 3 |
And I'm looking for a SQL statement that gives this result:
|----DATE----|---COUNTER---|
|2012-01-01 | 60 |
|2012-01-02 | 42 |
|2012-01-03 | 87 |
I've been working on this SELECT date, SUM(counter) FROM all_stats but all I get is:
|----DATE----|---COUNTER---|
|2012-01-01 | 60 |
Do I have to create a loop to go through all dates and count?
Thanks in advance, Steve-O
SELECT date, SUM(counter)
FROM all_stats
GROUP BY date
Try this instead
SELECT date, SUM(counter) FROM all_stats GROUP BY date;
"GROUP BY date" puts all the individual dates on a separate line and does the sum separately per date.
select date, sum(counter)
from all_stats
group by date