I want to hide the date which have been passed.
"SELECT * FROM " . USER_TABLE_NAME . " AS cu
INNER JOIN " . JOB_TABLE_NAME . " AS jb
ON jb.user_id = cu.user_id
WHERE cu.user_id = '$userID'
ORDER BY jb.job_date DESC, jb.job_tea_time DESC";
Rightnow I'm getting all the data order by date.
The date is stored in VARCHAR and i am not suppose to change that.
Thanks,
I think this will be helpful
"SELECT * FROM " . USER_TABLE_NAME . " AS cu
INNER JOIN " . JOB_TABLE_NAME . " AS jb
ON jb.user_id = cu.user_id
WHERE cu.user_id = '$userID' AND STR_TO_DATE(jb.job_date,'%d/%m/%Y') >= now()
ORDER BY jb.job_date DESC, jb.job_tea_time DESC;
You can use strtodate function to convert the string to date - afterwards you can easily compare it via date functions.
You can convert date string (varchar) to unix timestamp using mysql function unix_timestamp and for comparison less than today's date , you can use strtotime('today') in php
"SELECT * FROM " . USER_TABLE_NAME . " AS cu INNER JOIN " . JOB_TABLE_NAME . " AS jb
ON jb.user_id = cu.user_id
WHERE cu.user_id = '$userID' AND unix_timestamp(jb.jobdate) > " . strtotime('today') . "
ORDER BY jb.job_date DESC, jb.job_tea_time DESC";
This should work. If found any issue, let me know!
Related
I am new in CodeIgniter, I want to count all rows from database table but i use limit in query and i want all count without use limit how can i do ?
my code is below :
$sql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC
LIMIT $start, $per_page ";
$query = $this->db->query($sql)
i use limit for pagination but i want to get all record from table.
You can add new column in both above and below UNION queries. It will be like below.
select (select count(*) from your_query), your_columns from query_above_union
UNION
select (select count(*) from your_query), your_columns from query_below_union
your_query = your full actual query your are using currently.
Although I am not sure about Codeigniter. But sure about SQl.
* If you count all records with all data including limit, than you can use this code. please check it. I hope it will works for you.*
$countsql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC";
$sql = $countsql. " LIMIT $start, $per_page";
$totalRecords = $this->db->query($countsql);
$result["total_rows"] = $totalRecords->num_rows();
$query = $this->db->query($sql);
$result["list"] = $query->result_array();
This question already has answers here:
MySQL Check if date range is in date range
(2 answers)
mysql : check date range
(2 answers)
Closed 4 years ago.
I have the following code, and it's working fine:
$time = time();
$todayint=date("Y-m-d", strval($time));
$sql = "SELECT * FROM " . $DB->prefix('cal_event') . " WHERE DATE(FROM_UNIXTIME(event_start))='$todayint' OR DATE(FROM_UNIXTIME(event_end))='$todayint' ORDER BY event_organiser ASC";
How do I change the code to select data between a date range?
event_start and event_end are stored as int.
I tried using between but I'm not sure where exactly to put between in the code.
For queries against a single date field, you can make use of the BETWEEN operator:
$sql = "SELECT * FROM " . $DB->prefix('cal_event') . " WHERE
DATE(FROM_UNIXTIME(event_time)) BETWEEN '$date1' AND '$date2'
ORDER BY event_organiser ASC";
And for queries against multiple date fields (like both the start time and end time), you'll want to check => and <=:
$sql = "SELECT * FROM " . $DB->prefix('cal_event') . " WHERE
DATE(FROM_UNIXTIME(event_start)) >= '$todayint' AND
DATE(FROM_UNIXTIME(event_end)) <= '$todayint'
ORDER BY event_organiser ASC";
In both cases you'll want to make use of AND rather than OR.
Using BETWEEN (column BETWEEN value1 AND value2)
$time = time();
$todayint=date("Y-m-d", strval($time));
$sql = "SELECT * FROM " . $DB->prefix('cal_event')
. "WHERE DATE(FROM_UNIXTIME(event_start))
BETWEEN '$todayint' AND '$todayint'
ORDER BY event_organiser ASC";
Using >= (column >= value1 AND column <= value2)
$time = time();
$todayint=date("Y-m-d", strval($time));
$sql = "SELECT * FROM " . $DB->prefix('cal_event')
. "WHERE DATE(FROM_UNIXTIME(event_start)) >= '$todayint'
AND DATE(FROM_UNIXTIME(event_end)) <= '$todayint'
ORDER BY event_organiser ASC";
The select query below returns 1 row when it should be 3. I am pretty sure it is because of the AVG(k.sumtotal) field.
If I rewrite the query and take out that AVG(k.sumtotal) column and take out the FROM inv_ratings AS k, I get my 3 rows.
I can't figure out why this table (inv_ratings) or this AVG(k.sumtotal) column restrict the number of rows to 1. I looked online for hours trying to find information about returning results using the AVG clause and didn't find much. Do I have to use a group by clause, I tried that and only get errors.
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
$q = mysqli_query($dbc, $p) or trigger_error("Query: $p\n<br />mysqli Error: " . mysqli_error($dbc));
You are running into one of MySQL's gotchas:
http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
I hate that MySQL ever allows this syntax because it only causes confusion. But what you probably want is (to use MySQL's hackish behavior, please note that if there are multiple values for any of the fields besides invention_id or sumtotal, you get a random value from that column):
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
Or, to not use MySQL's hackish behavior:
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
I have written sql code using group by and order by but in here order by not working can any one help me
SELECT " . DB_PREFIX . "leaderboard_scores.* , SUM(" . DB_PREFIX . "leaderboard_scores.score) as total_score, " . DB_PREFIX . "customer.firstname,
" . DB_PREFIX . "customer.lastname
FROM " . DB_PREFIX . "leaderboard_scores
JOIN " . DB_PREFIX . "customer ON " . DB_PREFIX . "leaderboard_scores.philips_store_id = " . DB_PREFIX . "customer.customer_id
GROUP BY " . DB_PREFIX . "leaderboard_scores.phi_store_id
ORDER BY " . DB_PREFIX . "leaderboard_scores.week DESC
This query work without php errors but given middle rows without giving top weeks. weeks store using times stamp
actual query
SELECT rc_leaderboard_scores.* , SUM(rc_leaderboard_scores.score)
as total_score, rc_customer.firstname, rc_customer.lastname
FROM rc_leaderboard_scores
JOIN rc_customer
ON rc_leaderboard_scores.phi_store_id = rc_customer.customer_id
GROUP BY rc_leaderboard_scores.phi_store_id
ORDER BY rc_leaderboard_scores.week DESC
If you need to return the last record for every customer, you need a JOIN with a subquery that returns MAX(week) for every customer id:
SELECT
rc_leaderboard_scores.*,
m.total_score,
rc_customer.firstname,
rc_customer.lastname
FROM
(SELECT phi_store_id,
MAX(`week`) as max_week,
SUM(score) as total_score,
FROM rc_leaderboard_scores
GROUP BY phi_store_id) m
INNER JOIN
rc_leaderboard_scores
ON rc_leaderboard_scores.phi_store_id = m.phi_store_id
AND rc_leaderboard_scores.`week` = m.max_week
INNER JOIN
rc_customer ON m.phi_store_id = rc_customer.customer_id
I'm using a database to store logs, with a column "date" which holds the date it was inserted. The format of the date is "MM/DD/YY". Please can anyone suggest how I would SELECT data in between two certain dates. For example, I tried this:
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date . " AND date <= " . $to_date . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}
But I guess this doesn't work because the dates aren't numbers. Thanks for the help! :)
Use the BETWEEN keyword:
"SELECT * FROM logs WHERE date BETWEEN '" . $from_date . "' AND '" . $to_date . "'
ORDER by id DESC"
You can cast the fields as dates and then select between from_date and to_date
SELECT * FROM logs WHERE date STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE(from_date, '%m/%d/%Y') and STR_TO_DATE(to_date, '%m/%d/%Y')
The answer to your question depends on the data type that is used to store the date field in the logs table.
SQL (MySQL in your case) is fully capable of comparing dates. Usually, the BETWEEN .. AND .. operator is used but that will not work correctly if the type of date is CHAR (or VARCHAR) - in which case you will need to cast the date field to a DATETIME before comparing.
You need to add single quotes to the date values '01/01/12':
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= '" . $from_date . "' AND date <= '" . $to_date . "' ORDER by id DESC");
Change date parameters into Unix timestamps and then compare them. Here is the code:
$from_date = "2019/01/12";
$to_date = "2019/01/15";
$from_date_unix = strtotime($from_date);
$to_date_unix = strtotime($to_date);
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date_unix . " AND date <= " . $to_date_unix . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}