I am having trouble retrieving certain data from a db through a SELECT query as such:
SELECT table.something
FROM table
WHERE table.date BETWEEN 'from' AND 'to'
GROUP BY (each 6 months between the from and to date).
Any idea how this can be done without having to recur to a view and an external grouping through code.
Something like this will work:
SELECT
table.something,
CEIL(MONTH(date)/6) as monthVALUE
FROM
table
WHERE
table.date BETWEEN 'from' AND 'to'
GROUP BY
monthVALUE
Instead of worrying about on a single query create a temporary table insert record with the start date and end date and your some other column. and use the select query for each record to get the results and store it in the temporary table and select the temporary table.
Related
Maybe this question has been answered somewhere else but I didn't know how can I apply that query in my working project and I am a beginner(new) to using MySQL.
I have 2 tables in my Phpmyadmin.
table-1 looks like this :
and table-2 looks like this
By using the below query I am able to join both tables together but in different column names.
SELECT DISTINCT `a`.`subject`,`b`.`subject`, `a`.`date`, `a`.`start_time`, `a`.`end_time`, `b`.`date`, `b`.`start_time`, `b`.`end_time`
from `time-table` as a
inner join `oral_time-table` as b on a.oral_token=b.token and a.class='EIGHTH' and a.medium='English' and a.faculty='GENERAL' and a.exam='Half-Yearly'
The below query results in this :
I want both data date name column in ascending order in both the tables. Therefore I need to join both the tables one by one.
With only one common column name both the table rows are joined. How can I achieve this and thanks in advance...
Desired Result :
date column ascending order.
You can achieve the desired result using UNION.
SELECT subject, `date`, start_time,
end_time
FROM TABLE-1
UNION
SELECT subject, `date`, start_time,
end_time
FROM TABLE-2
ORDER BY `date` ASC
Don't use date as any column name as it is a reserved keyword and is
not a good practice.
I tried making a SQL query and union the result on the current time, but I cannot seem to find a neat way to solve this.
I've tried the following:
SELECT * FROM `accounts`
UNION SELECT NOW()
And Sequel Pro just reports The used SELECT statements have a different number of columns.
The accountstable just has three columns:
ID (INT(32), AUTO_INC)
CREATED (Timestamp)
NAME (VAR_CHAR(28))
I anticipated I'd get a response with four columns: ID, CREATED, NAME, NOW
What do I do wrong?
Union means that the records from the second query will be appended to those retrieved from the first one.
So the two tables must have the same structure for this to work.
For example:
SELECT field1,field2,field3 FROM tableA
UNION
SELECT field1,field2,field3 FROM tableB
What you want to do is
SELECT *, NOW() as now FROM `accounts`
This will retrieve all the records from the accounts table and will add the timestamp to all the rows on a column named "now" (this is just an alias so use whatever you like).
try this
SELECT *,now() as now FROM `accounts`
I've been messing around with MySQL lately and I can't resolve one problem.
Having 2 tables.
status_graf(id,date,time,number of players online)
status_statistika(id,date,min,avg,max)
First one is storing data about online players every 5 minutes, second one is for daily statistics. It's separated, so I don't have to run crazy selects whenever man wants to see min,avg,max online players for last month for example.
This query works, result is a table with dates that haven't been in status_statistika yet.
SELECT date,MIN(playersonline),AVG(playersonline),MAX(playersonline)
FROM status_graf
GROUP BY date
HAVING date NOT IN (
SELECT date
FROM status_statistika
GROUP BY date
)
But when I try this
INSERT INTO status_statistika (
SELECT date,MIN(playersonline),AVG(playersonline),MAX(playersonline)
FROM status_graf
GROUP BY date
HAVING date NOT IN (
SELECT date
FROM status_statistika
GROUP BY date
)
);
it does not.
Using phpMyAdmin 4.3.8, MySQL 5.5.41 and it throws error #1062 - Duplicate entry '2015-01-29' for key 'date'.
Ideas?
SOLUTION:
Since the dot convention somehow didn't accept it, I had to rename column 'date' in one table to 'day' and then it worked
Looks like you have unique index on the date column in the status_statistika table.
INSERT INTO status_statistika (
SELECT DISTINCT date,MIN(playersonline),AVG(playersonline),MAX(playersonline)
FROM status_graf
GROUP BY date
HAVING date NOT IN (
SELECT date
FROM status_statistika
GROUP BY date
)
);
Notice the 'DISTINCT'
I am not sure if this is possible but here goes.
I am using MySQL and need to complete a statement that produces an output as follows.
I have a table which contains a completeddate field and an enquirydate field.
I need to get data on the time difference between theses fields for which I have the following code
SELECT DATE (completedate) - DATE (enqdate) AS `timediff`, COUNT(*) AS TotalCount
FROM cia_enquiry
WHERE (DATEDIFF(CURDATE(), enqdate) < 30) AND completedate > 1
GROUP BY `timediff`
ORDER BY `timediff` ASC
The above code simply outputs a figure of days between the dates and the total count of entires within the last 30 days (and not counting those entries that are not complete)
I now need to reference the "timediff" results against another table and pull another field from that table.
For example if timediff = 1, then we need to find the id of 1 in another table and return the description field from this table.
Ultimately we should end up with something similar to
timediff TotalCount description
0 52 Within 24 hrs
1 13 24-48hrs
etc etc .........
It can be done; you have two choices
Create a temporary table with the result of your query and then join this table with your second table
Use your first query as a subquery and join it with your second table
Option 1
create temporary table temp_tbl
select
...
;
# Important: create the indexes you need
alter table temp_tbl
add index index1(field1), ... ;
# Now join your second table
select a.*, b.*
from temp_tbl as a join tbl2 as b on ...;
Option 2
select a.*, b.*
from (
select ... # here is your query
) as a join tbl2 as b on ...
Hope this helps you
The date calculation will give you a bunch of decimal points worth of values...
you should use ROUND()
or look up CEIL and FLOOR functions.
I have a time log table where all entries are entered with a time stamp and an event.
Now I want to select all the rows after a specific event AND ONE row before(ORDER BY time_stamp) that event.
I can easily achieve this with multiple queries, but is it possible with only one query ?
Using multiple queries
SELECT time
FROM table
WHERE event LIKE '%event_to_fint%'
SELECT event
FROM table
WHERE time<'time from last query'
LIMIT 1
ORDER BY
time DESC
One option is to
Use a regular select to select the time records you need.
Union this result with a select that retrieves the maximum time where this maximum time is less than the minimum time from your regular select.
SQL Statement
SELECT time
FROM table
WHERE event LIKE '%event_to_fint%'
UNION ALL
SELECT MAX(time)
FROM table
WHERE time < (
SELECT MIN(time)
FROM table
WHERE event LIKE '%event_to_fint%'
)
This solution in not too nice, but works:
SELECT *
FROM table
WHERE <condition_A>
ORDER BY <condition_B>
LIMIT (SELECT COUNT(*)
FROM table
WHERE <condition_A>
)+1