I have a system that shows records year wise. I have dropdown menu from which user selects year, and I am storing that value in variable $year. So now when $year = 2013-2014, it should not display records of year 2014-2015. BTW when I insert my contractNumber, I also insert the year which user has selected during the login. e.g. contractNumber = 1#2013-2014.
I tried this queries:
SELECT * FROM contract WHERE contarctNumber RLIKE "^$year";
Now when I select year 2014-2015, it still displays all the records from year 2013-2014.
Any suggestions?
Try this query
SELECT * FROM contract WHERE contarctNumber LIKE "%$year";
SQLFiddle DEMO
Example
CREATE TABLE test (contarctNumber varchar(25));
INSERT INTO test VALUES ('1#2013-2014'), ('1#2014-2015'), ('2#2014-2015');
Select Query
SELECT * FROM test WHERE contarctNumber LIKE "%2014-2015";
Output
| CONTARCTNUMBER |
------------------
| 1#2014-2015 |
| 2#2014-2015 |
If the contractNumber always end in the date range, why not use:
SELECT * FROM contract WHERE contarctNumber LIKE "%$year";
This means it will match anything before the $year selected but still match the range specified.
Option #2:
SELECT * from contract where RIGHT(contarctNumber, 9) = '$year';
Related
The scenario is I am the admin and I have a session to make, the session table consists of a session_id, coach_name, date(format Y-m-d).
Now I want to count the total activity of the coach every "MONTH".
So my point is how will I get this kind of output in a SQL query.
The desired output is the amount of activity per month, per coach.
Name | No. of session this month
Coach_name1 = 13
Coach_name2 = 5
This is my current query that gives me the coach name but I don't know how to get for each MONTH
public function getAll(){
$query = $this->db->query('SELECT DISTINCT coach_name FROM sessions);
return $query->result();
}
Use this query to get the result.
Use mysql MONTH() to get Month number from date and group by it. You will get all month wise data.
SELECT coach_name,
Count(*) AS activities
FROM sessions
WHERE Month(date) = 8 //for specfic month
GROUP BY coach_name, Month(date) // for all months
Use this query to dynamincally get current month based on system date
SELECT coach_name,
Count(*) AS activities
FROM sessions
WHERE Month(date) = MONTH(CURRENT_DATE()) //for selecting current month.
GROUP BY coach_name
You have to prepare query like below:
select
coach_name,
month(your_date_field),
count(1)
from
your_table
where
month(your_date_field) = 8
group by
coach_name, month(your_date_field)
Which would return records like below,
| coach_name | month(your_date_field) | count(1) |
+---------------+------------------------+------------+
| Coach_name1 | 8 | x |
| Coach_name2 | 8 | x |
| Coach_name3 | 8 | x |
+---------------+------------------------+------------+
In codeigniter
$this->db->select("coach_name, month(your_date_field) as month_number,count(1) as counts");
$this->db->where('month(your_date_field)',8);
$this->db->group_by('coach_name,MONTH(your_date_field)');
$query = $this->db->get('your_table');
// results
foreach ($query->result_array() as $row)
{
// do this to see array
print_r($row);
// to access individual fields
echo $row['coach_name'];
echo $row['month_number'];
echo $row['counts'];
}
Use MySQL's MONTH() in your query. So basically if all your data is in your sessions table try the following in your query.
SELECT DISTINCT(`name`),COUNT(`name`) AS `activity` FROM `session`
WHERE MONTH(`date`)=8 GROUP BY `name`;
Check in SQLFiddle
I have three table Like this:
members_tbl
id | Fullname | Email | MobileNo
attendance_in_tbl
id | member_id | DateTimeIN
attendance_out_tbl
id | member_id | DateTime_OUT
I want to select all members for date: 2014-03-10 by this query:
SELECT
attendance_in.EDatetime,
members_info.mfullname,
attendance_out.ODatetime
FROM
attendance_in
LEFT JOIN members_info ON members_info.id = attendance_in.MemID
LEFT JOIN attendance_out ON attendance_out.MemID = attendance_in.MemID
WHERE date(attendance_in.EDatetime) OR date(attendance_out.ODatetime) = "2014-03-10"
But it give me different results in Attendace_out Results
You have a mistake in your query.
You wrote:
WHERE date(attendance_in.EDatetime) /* wrong! */
OR date(attendance_out.ODatetime) = "2014-03-10"
This is wrong, as the first expression date(attendance_in.EDatetime) always evaluates to true.
You may want
WHERE date(attendance_in.EDatetime) = "2014-03-10"
OR date(attendance_out.ODatetime) = "2014-03-10"
But, this is guaranteed to perform poorly when your attendance_in and attendance_out tables get large, because it will have to scan them; it can't use an index.
You may find that it performs better to write this:
WHERE (attendance_in.EDatetime >='2014-03-10' AND
attendance_in.EDatetime < '2014-03-10' + INTERVAL 1 DAY)
OR (attendance_out.EDatetime >='2014-03-10' AND
attendance_out.EDatetime < '2014-03-10' + INTERVAL 1 DAY)
That will check whether either the checkin our checkout time occurs on the day in question.
I have two tables, and I want to get the last enterd date.
The first table is seeker:
seeker_nic-----username
111-------------ali
222-------------umer
333-------------raza
The second one is requestblood:
id-------seeker_nic-----requireddate
1------- 111 ----------2012/10/9
2 ------- 222-----------2012/5/8
3 ------ 111-----------2012/12/12
4 ------- 111-----------2012/11/12
5---------111-----------2012/09/09
6 ------- 222-----------2012/7/9
7 ------- 333 ----------2012/4/4
Now, I want to list the users with their last inserted date like..
s.no---- username----- requireddate
1------- ali---------- 2012/09/09
2------- umer--------- 2012/7/9
3------- raza--------- 2012/4/4
i am using this query
SELECT seeker.username,bloodrequest.requireddate,
max( bloodrequest.bloodrequest_id ) AS maxdate
FROM seeker
JOIN bloodrequest ON seeker.seeker_nic = bloodrequest.seeker_nic
GROUP BY seeker.username
when i run this query in phpmyadmin.. its lists all user and show the first inserted date of each user.. but i want the last inserted date ... what should i do.. please i need help :(
EDIT for real answer:
Not very clean sql, but it will get you the results you are looking for. I'm sure there's a way do it better with group by, but this works. :)
SELECT s.username,
(
SELECT br1.requireddate
from bloodrequest as br1
where br1.bloodrequest_id =
(
select max(br2.bloodrequest_id)
from bloodrequest as br2
where br2.seeker_nic = s.seeker_nic
)
) as requireddate
FROM seeker as s
I want to know how to display report by month using PHP and Mysql
Example of Report on the web page:
January 2011
==============
Store Name | Total Order Cost
SHOP A | £123
SHOP B | £100
February 2011
==============
Store Name | Total Order Cost
SHOP A | £123
SHOP B | £100
SHOP C | £99.40
I have mysql tables
tbl_shop
ShopID
ShopName
tbl_order
OrderID
ShopID
OrderDate
Total
You will need to iterate trough the result of the query and create an multidimensional array using the month/year combination as keys. The query below should be a good indication on how to fetch the required information from your database.
SELECT
MONTH(to.OrderDate),
YEAR(to.OrderDate),
SUM(to.Total),
to.*
FROM tbl_order as to
INNER JOIN tbl_shop as ts ON ts.ShopID = to.ShopID
GROUP BY to.ShopID, MONTH(to.OrderDate), YEAR(to.OrderDate)
Note that I havn't tested this query - please handle it as pseudo-code. You might need to throw around the GROUP BY fields a little and test if it works.
Use
SELECT ShopName,SUM(Total),MONTHNAME(OrderDate) from tbl_shop
left join tbl_order
on tbl_shop.ShopID = tbl_order.ShopID
GROUP By(ShopID),MONTHNAME(OrderDate)
will give you All months report.
Use this query:
SELECT monthname(o.OrderDate) as Month, year(o.OrderDate) as Year,
s.ShopName, sum(o.Total) as Total
FROM tbl_Shop s JOIN tbl_Order o
ON s.ShopID=o.ShopID
GROUP BY o.shopID,year(o.Orderdate),month(o.Orderdate)
ORDER BY year(o.OrderDate),month(o.OrderDate)
then for every row from result if month+year has changed (a variable $last_date could help) then print
$Month $Year
==============
Store Name | Total Order Cost
$ShopName | $Total
else print
$ShopName | $Total
I have a table which stores bookings of rooms, the schema is:
ID | ROOM_ID | CHECK_IN_DATE | CHECK_OUT_DATE | USER_ID
I need to run a search query for rooms which are available/unavailable between a set range of dates.
Also keep in mind that there exists another table which holds dates when the room is prebooked and its in the format:
ROOM_ID | DATE
SO I need to run a query which looks for rooms available within a set range, How would I formulate the query?
I'm using MySQL here.
---edit---
Theres also a Rooms table of the schema:
ID | ROOM DETAILS etc
The unavailability/prebooked dates table basically holds sporadic single dates, each date in the unavailability table refers to a date when the room for some reason cannot be booked eg: maintenance etc
SELECT
ROOM_ID
FROM
Rooms r
LEFT JOIN Bookings b ON (
r.ROOM_ID = b.ROOM_ID
AND b.CHECK_IN_DATE > '$MAX_DATE'
AND b.CHECK_OUT_DATE < '$MIN_DATE'
)
I'm not sure how pre-booked rooms factors in as there is no date range. Do pre-booked rooms also get an entry on bookings or not?
SELECT id FROM rooms WHERE id NOT IN (
SELECT room_id FROM bookings
WHERE check_in_date < 'end date' AND check_out_date > 'start date'
);
there are like 5 possible ways:
---s-----e---
s-e----------
--s-e--------
-----s-e-e---
--------s-e--
----------s-e
s = start / e=end firste line is your database set and the other are possible searches
only the first and last one are the ones that you want so what you look for is:
search.end < entry.start OR search.start > entry.end