I have two tables in MySQL:
Table 1
Week From Until
1 2015-04-01 2015-04-07
2 2015-04-08 2015-04-14
3 2015-04-15 2015-04-21
4 2015-04-22 2015-04-28
Table 2
Input_Date Code
2015-04-10 123
2015-04-22 456
2015-04-25 123
2015-04-26 123
I used this query to select the current week based on the current date:
SELECT Week FROM table_1 WHERE (NOW() BETWEEN From AND Until)
I need to select the "code" and count it from table 2 where "Code" = 123 and "Input_Date" corresponds to the current "Week".
*If the current date is 2015-04-23, the "Week" would be = 4
The result would be:
Week Code Count
4 123 2
Try-
SELECT t1.Week, t2.Code, COUNT(*)
FROM table_1 t1
LEFT JOIN table_2 t2
ON t2.Input_Date BETWEEN t1.From AND t1.Until
WHERE (NOW() BETWEEN t1.From AND t1.Until)
AND t2.Code = 123
http://sqlfiddle.com/#!9/3d2a6/16
Alternate way of doing the same thing without LEFT JOIN
SELECT a.week, b.code, count(a.week)
FROM table_1 a, table_2 b
WHERE (b.input_date BETWEEN a.from AND a.until)
AND (NOW() BETWEEN a.From AND a.Until)
AND b.code = 123
Related
id user_id name qty datetime
--- --------- ---- ---- -----------
1 1 a 5 2019-12-01 12:26:01
2 2 b 3 2019-12-13 12:26:02
3 1 c 4 2019-12-13 12:26:03
4 2 a 2 2019-12-25 12:26:04
5 1 c 2 2019-12-21 12:26:06
i Want the this data
id user_id name qty datetime
--- --------- ---- ---- -----------
5 1 c 2 2019-12-21 12:26:06
4 2 a 2 2019-12-25 12:26:04
using laravel and also if possible then what will be the sql query for it
Models:
Users: id, name, email, etc...
Orders: user_id, qty, name, datetime etc..
Model Query:
Orders::orderBy('datetime', 'desc')->get()->unique('user_id');
DB Query
DB::table('orders')->orderBy('datetime', 'desc')->get()->unique('user_id');
In pure SQL, you can filter with a correlated subquery:
select t.*
from mytable t
where t.datetime = (
select max(t1.datetime) from mytable t1 where t1.user_id = t.user_id
)
or an uncorrelated subquery...
select x.*
from mytable x
join (
select user_id, max(t1.datetime) datetime from mytable group by user_id
) y
on y.user_id = x.user_id
and y.datetime = x.datetime
I have 3 tables like below :
hr_emp_job_compensation:
id date fkEmp_id basic_wage part_hours part_amt
1 04-01-2016 1 4500 35 120
2 04-01-2016 3 3800 30 150
3 08-01-2016 3 3200 30 100
hr_emp_job_info:
id fkEmp_id
1 1
2 3
hr_emp_info:
id employee_id first_name
1 001 Ram
2 002 Lak
3 003 jai
4 004 shiva
I want to select records from table 1 , based on the column Date value is higher.
I Try the following query :
SELECT t1.fkEmp_id,max(t1.date),max(t1.id) as uid,t1.part_hours,t1.part_amt, t3.first_name, t3.employee_id
FROM `hr_emp_job_compensation` as t1
inner join `hr_emp_job_info` as t2 on t1.fkEmp_id = t2.fkEmp_id
left join `hr_emp_info` as t3 on t3.id = t1.fkEmp_id
group by t1.fkEmp_id
But the result is look like below :
fkEmp_id max(t1.date) uid part_hours part_amt first_name employee_id
1 2016-01-04 1 35 120 Ram 001
3 2016-01-08 3 30 150 Jai 003
Here the part_hours and part_amt columns are fetched from the id 2. How to change the query.
No need to add MAX() for the dateand id. You can handle the MAX(date) in the WHERE clause.
SELECT t1.fkEmp_id, t1.date as `date`, t1.id as uid,
t1.part_hours, t1.part_amt,
t3.first_name, t3.employee_id
FROM `hr_emp_job_compensation` as t1
INNER JOIN `hr_emp_job_info` as t2 on t2.fkEmp_id = t1.fkEmp_id
LEFT JOIN `hr_emp_info` as t3 on t3.id = t1.fkEmp_id
WHERE t1.`date`= ( SELECT MAX(`date`)
FROM `hr_emp_job_compensation`
WHERE fkEmp_id = t1.fkEmp_id);
Please find the Working Demo
In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .
There are 3 tables: buildings, rooms, reservations.
1 building = n rooms
1 room = n reservations
TABLE BUILDINGS - ID(int), name(varchar)
TABLE ROOMS - ID(int), building_id(int)
TABLE RESERVATIONS - ID(int), room_id(int), date_start(datetime), date_end(datetime)
Buildings table example
<pre>
ID name
1 Building A
2 Building B
3 Building C
</pre>
Rooms table example
<pre>
ID building_id
1 1
2 1
3 2
4 3
</pre>
Reservations table example
<pre>
ID room_id date_start date_end
1 1 2014-08-09 14:00:00 2014-08-09 14:30:00
2 1 2014-08-09 14:30:00 2014-08-09 15:30:00
3 3 2014-08-09 16:30:00 2014-08-09 17:30:00
4 2 2014-08-09 16:00:00 2014-08-09 17:00:00
5 3 2014-08-09 16:00:00 2014-08-09 16:30:00
</pre>
Question
How to filter all buildings which have atleast 1 room available at the specific date and time?
For example we want to filter all buildings which have atleast 1 available room at 2014-08-09 16:00:00 until 2014-08-09 17:00:00. In this case it will show that Building A AND Building C are available. Building A has 1 free room and Building B also has 1 free room, because there is no reservation on that room.
Can someone help me out on this one? I just can not figure it out with a single MySQL query to get all buildings. Thank you.
Use LEFT JOIN to connect the rooms table to the reservations table, with the time condition in the on clause. Then, look where there are no matches -- these are rooms that are available.
SELECT b.id, b.name, COUNT(*) as NumRoomsAvailable
FROM buildings b JOIN
rooms r
ON b.id = r.building_id LEFT JOIN
reservations rv
ON rv.room_id = r.id AND
'2014-08-09 16:00:00' BETWEEN rv.date_start AND rv.date_end
WHERE rv.room_id is null
GROUP BY b.id;
The final step is just aggregating by the building, rather than listing each available room out separately.
Try this query
SELECT b.name,r.id,rv.date_start,rv.date_end FROM buildings b
LEFT JOIN rooms r ON b.id = r.building_id
LEFT JOIN reservataions rv ON rv.room_id = r.id
WHERE '2014-08-09 16:00:00' BETWEEN rv.date_start AND rv.date_end
Here is a simplified version of my sql table of 2 months (ORDERED BY DATE):
player_id |
date |
score
1 2011-05-25
1200
2 2011-05-25
3400
3 2011-05-26
3200
4 2011-05-26
4400
1 2011-05-28
1000
2 2011-05-28
2000
3 2011-05-29
3000
4 2011-05-29
4000
1 2011-06-24
1300
2 2011-06-24
2500
3 2011-06-24
5000
4 2011-06-24
3000
Basically, I want a query that shows the last score of all players in a specific month/specific year.
Example:
If I want the final scores of all players in the month 05, te result
would be:
1 2011-05-28 1000
2 2011-05-28 2000
3 2011-05-29 3000
4 2011-05-29 4000
My sql query so far:
SELECT m1.* FROM table m1
LEFT JOIN table m2 ON (m1.player_id = m2.player_id AND m1.date < m2.date)
WHERE m2.date IS NULL
AND month(m1.date) = 05
AND year(m1.date) = 2011
ORDER BY score DESC);
This doesn't seem to show all players, only players that didn't play in the months after 05. Where do I add the date select?
**EDIT
John Nestoriak's answer bellow did the trick for me :)
I think he's referring to the technique shown here: Retrieving the last record in each group
With the additional constraint of he doesn't want the last record but the last record in a given month.
Oddly enough you have to give that additional constraint twice, once in the join condition and again to filter the results. This should do it for you.
SELECT m1.* FROM table m1
LEFT JOIN table m2 ON
(m1.player_id = m2.player_id AND m1.date < m2.date
AND m2.date < '2011-06-01')
WHERE m2.date IS NULL AND month(m1.date) = 5 AND year(m1.date) = 2011
Assuming that the (player_id, date) combination in Unique:
SELECT
t.*
FROM
TableX AS t
JOIN
( SELECT
player_id
, MAX(date) AS maxDate
FROM
TableX
WHERE
date BETWEEN '2011-05-01'
AND LAST_DAY('2011-05-01')
GROUP BY
player_id
) AS tg
ON
(tg.player_id, tg.maxDate) = (t.player_id, t.date)
ORDER BY
t.score DESC