Query calculating dates with INNER JOIN

Asked

Viewed 155 times

-2

Personal hail, Here there’s an example that a guy here from the community posted and I’m trying to adapt my needs but an error arises:

#1248 - Every derived table must have its own alias

My query

SELECT
 date_format(TABLE_2.data, '%d/%m/%Y %H:%i:%s') as DATA_2,
 date_format(TABLE_1.data, '%d/%m/%Y %H:%i:%s') as DATA_1, 
 Anos, 
 Mes, 
 Dias,
 Horas,
 Minutos
FROM TABLE_1
INNER JOIN
(
 SELECT
 data,
 TIMESTAMPDIFF(YEAR, data, TABLE_1.data) as Anos,
 TIMESTAMPDIFF(MONTH, data + INTERVAL TIMESTAMPDIFF(YEAR,  data, TABLE_1.data) YEAR , TABLE_1.data) AS Mes,
 TIMESTAMPDIFF(DAY, data + INTERVAL TIMESTAMPDIFF(MONTH,  data, TABLE_1.data) MONTH , TABLE_1.data) AS Dias,
 TIMESTAMPDIFF(HOUR, data + INTERVAL TIMESTAMPDIFF(DAY,  data, TABLE_1.data) DAY , TABLE_1.data) AS Horas,
 TIMESTAMPDIFF(MINUTE, data + INTERVAL TIMESTAMPDIFF(HOUR,  data, TABLE_1.data) HOUR , TABLE_1.data) AS Minutos
 FROM TABLE_2
)
ON TABLE_2.id = TABLE_1.id

I need to do the same as the example only using 2 tables and with sum of hours and minutes.

Thank you in advance for your attention

  • Have you researched the error Every derived table must have its own alias? Try putting a alias after the parentheses in INNER JOIN. Ex.: tbl2. And then on ON, refer to alias servant. tbl2.id = TABLE_1.id.

1 answer

2


You need a alias in the select internal. Anyway, your query is wrong. TABLE_1.data makes no sense inside the select internal. Try something like

SELECT
 date_format(TABLE_2.data, '%d/%m/%Y %H:%i:%s') as DATA_2,
 date_format(TABLE_1.data, '%d/%m/%Y %H:%i:%s') as DATA_1, 
 TIMESTAMPDIFF(YEAR, data, TABLE_1.data) as Anos,
 TIMESTAMPDIFF(MONTH, data + INTERVAL TIMESTAMPDIFF(YEAR,  data, TABLE_1.data) YEAR , TABLE_1.data) AS Mes,
 TIMESTAMPDIFF(DAY, data + INTERVAL TIMESTAMPDIFF(MONTH,  data, TABLE_1.data) MONTH , TABLE_1.data) AS Dias,
 TIMESTAMPDIFF(HOUR, data + INTERVAL TIMESTAMPDIFF(DAY,  data, TABLE_1.data) DAY , TABLE_1.data) AS Horas,
 TIMESTAMPDIFF(MINUTE, data + INTERVAL TIMESTAMPDIFF(HOUR,  data, TABLE_1.data) HOUR , TABLE_1.data) AS Minutos
FROM TABLE_1
INNER JOIN TABLE_2
ON TABLE_2.id = TABLE_1.id;
  • thanks for the help brother. now arises the error #1054 - Unknown column 'TABLE_1.data' in 'field list'

  • TABLE_1 has the date column?

  • Yes you have........

  • @Hugo the problem is that TABLE_1.data does not make sense in the internal select. had not realized it.

  • And how can I fix it @Brunocalza :(

  • @Hugo edited the answer. provide more information about your structure so we can help you

  • now it’s gone! vlw Bruno

  • with this information above would give to catch the overall average time?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.