Skip to content

Tag: mysql

Limit total number of results across tables

I want to get the most recent 100 events that happened. The events are scattered across multiple tables. Here is an example: This will return up to 300 records. I would then take the result set, order by timestamp, and take the first 100 records. How can I perform this in a single SQL query, where SQL will on…

Inner join 2 tabelas and get last result

I’m having trouble writing the query that returns the last id by date of a table and fetching the name of that id from another table Result above 2| 1 |2014-04-04 | ok 6| 2 |2014-04-04 | ok 4| 3 |2014-04-04 | ok ficha_cadastro SQ_FICHA|NOME| 1|gabriel 2|carlos 3|manuel ficha_evento id|dia_atendimento|li…

Multiple JOINs to the same subquery expression

I’ve recently been teaching myself SQL, and have been working on a toy project to do so. Here is a sample schema: As part of this project, I have a query that gets a list of users, with information populated relative to the currently authenticated user. A simplified version of the query looks like this:…

mysql: Select all rows and compare with other table

I have two tables and need to get all rows from the first one and then check which values from the second table match the first table. My goal is to get all so called ‘achievements’ and then check which one the user has reached. achievements achievement_user My desired result for a query where I c…

Order by multiple columns not working as expected

I have this sql : The result is : Expected result : But I want to order and by month not only by year. I tried like this ORDER BY dc.year DESC, dc.month ASC, d. user_id ASC, but not working as expected because I want to liste each user order by year desc and by month asc. Can you help