Hiii i have two diffrent tables
staff_rotas (id,staff_id, startDateTime, endDateTime, rota_minutes, status)
staff_login (id,staff_id, actual_startDateTime,actual_endDateTime,shift_minutes, status);
I want to fetch data from both table where date between e.g (21-11-2016 to 27-11-2016) and output something like this
Timesheet
I have tried following query pls check.
<?php
$staff_data = mysql_query("SELECT * FROM staff_rotas LEFT JOIN staff_login ON
(DATE(staff_rotas.startDateTime) = DATE(staff_login.actual_startDateTime))
WHERE staff_rotas.staff_id = '123' AND DATE_FORMAT(staff_rotas.startDateTime,'%Y-%m-%d')
BETWEEN '" . $date_from . "' AND '" . $date_to . "'
UNION
SELECT * FROM staff_rotas RIGHT JOIN staff_login ON (
DATE(staff_login.actual_startDateTime) = DATE(staff_rotas.startDateTime))
WHERE staff_login.staff_id = '123' AND
DATE_FORMAT(staff_login.actual_startDateTime,'%Y-%m-%d') BETWEEN '" . $date_from . "' AND '" . $date_to . "'
ORDER BY `startDateTime` DESC")
while($data = mysql_fetch_object($staff_data)){
/*html table to show data ......
...
...
*/
}
?>
Related
I am new to Codeigniter 4 and trying to write SELECT UNION SQL Statements. According to the user guide in codeigniter 4, there is a second parameter in select() method that allows to write custom SQL Statements by setting it to false. Unfortunately it is not working. Can someone help please? Is there any alternate solution?
$query = "SELECT acnt_id, acnt_opbal as sum_amount
FROM accounts
WHERE acnt_id = '" . $acnt_id . "'
UNION
SELECT txn_acnt_id_dr as acnt_id, SUM(txn_amount_dr) as sum_amount
FROM transactions
WHERE txn_acnt_id_dr = '" . $acnt_id . "'
AND txn_date < '" . $fdate . "'
GROUP BY txn_acnt_id_dr
UNION
SELECT txn_acnt_id_cr as acnt_id, -SUM(txn_amount_cr) as sum_amount
FROM transactions
WHERE txn_acnt_id_cr = '" . $acnt_id . "'
AND txn_date < '" . $fdate . "'
GROUP BY txn_acnt_id_cr";
$result = $this->builder()->select($query, false)->get()->getResult();
According to CodeIgniter 4 Documentation ->getResult() should be used after building the query in order to produce your query
<?php
$builder = $db->table('mytable');
$query = $builder->get(); // Produces: SELECT * FROM mytable
foreach ($query->getResult() as $row) {
echo $row->title;
}
Addapting this to your code you'll get:
<?php
$builder = $this->builder()->select($query, false);
$query = $builder->get();
$result = $query->getResult();
Or make it all 1 line you can use Covariance and Contravariance:
$result = ($this->builder()->select($query, false)->get())->getResult();
I am working in OpenCart and am trying to change this query. I need to change it so that it returns records from that last year(1 year from now) here is my query
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND YEAR(date_added) = YEAR(CURDATE()) GROUP BY channel");
I tried changing it to this but it ddidnt work:
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND date_added BETWEEN CURDATE() - INTERVAL 1 year AND CURDATE()) GROUP BY channel");
Before, consider to sanitize your input data to protect your queries against SQL Injection. Here you can find some content about this topic.
Based on this answer, we can adapt your PHP code to the following:
$query = $this->db->query("SELECT count(*) AS total, channel
FROM `" . DB_PREFIX . "order`
WHERE customer_id = '" . (int)$customer_id . "'
AND order_status_id IN(" . implode(",", $implode) . ")
AND date_added >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
GROUP BY channel");
You got the wrong syntax when calculating the last year.
You can use DATE_SUB.
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND CURDATE()) GROUP BY channel");
I am using codeigniter to build an app and I am really far in the code.
I have a table with column headers id, page, user and like. Now the problem is, in the mysql query, I realised I cant use the word like for the column name as its a sql keyword I belive.
I can't change the column name from like to something else because it would mean changing 100s of lines of php code.
Is there something that i can do to overcome the clash of the world like?
here is what I mean
$like_variable = 100;
$query = $this->db->query("SELECT * FROM `table_name`
WHERE id ='" . $qry . "' AND
page = '" . $_SESSION['p_id'] . "' AND
user_id = '" . $_SESSION['user_id'] . "' AND
like = '" . $like_variable . "'
");
// i think i cant use the world like above but I cant change the column header
Any solutions would be much appreciated thanks in advance
In SQL, you can use keywords as column names. Wrap them in ``.
SELECT * from `table_name` WHERE `like` = 100
Change like to:
`like`
Result:
$like_variable = 100;
$query = $this->db->query("SELECT * FROM `table_name`
WHERE id ='" . $qry . "' AND
page = '" . $_SESSION['p_id'] . "' AND
user_id = '" . $_SESSION['user_id'] . "' AND
`like` = '" . $like_variable . "'
");
I have two MySQL table for insert comment data and reviews data like this :
comments:
|id|post_id|text|name|timestamp|approved|parent_id|type|ip|
reviews:
|id|postID|comments_id|reviewfield1|reviewfield2|reviewfield3
in action I insert for each comments One reviews. Now, I need to show/print comment list with comment name and text + reviewfield1 reviewfield2 reviewfield3 for each comments where comments is approved like this :
commenter 1
text
reviewfield1
reviewfield2
reviewfield3
TRY:
"SELECT name,text,timestamp FROM " . COMMENTS . " LEFT JOIN " . REVIEWS . " WHERE " . COMMENTS . ".id = " . REVIEWS . ".comments_id AND
post_id = ? AND type = ? AND approved = 1 ", $id, $type"
But this not work for me. how do generate this with PHP JOIN method?
You are making mistake in the query. It must be like this:
"SELECT name,text,timestamp,reviewfield1 FROM " . COMMENTS . " LEFT JOIN " . REVIEWS . " ON " . COMMENTS . ".id = " . REVIEWS . ".comments_id WHERE
post_id = ? AND type = ? AND approved = 1 ", $id, $type"
And you can select rows from the second(joined) table, same as the first one.
the below query works and returns results
$query = "SELECT * FROM table WHERE District = '" . $var . "' ORDER BY Form_Date DESC";
where as if I substitute the word "District" with a variable, it doesn't work
$query = "SELECT * FROM table WHERE '" . $distvar . "' = '" . $var . "' ORDER BY Form_Date DESC";
what is wrong with this one and how can I make it work?
Remove the quotes surrounding the field you are testing for, or replace them with backticks to save you from the mysql parser mistaking it for a potentially reserved word:
$query = "SELECT * FROM `table` WHERE `" . $distvar . "` = '" . $var . "' ORDER BY Form_Date DESC";
try this :
$query = "SELECT * FROM table WHERE `" . $distvar . "` = '" . $var . "' ORDER BY Form_Date DESC";
or
$query = "SELECT * FROM table WHERE " . $distvar . " = '" . $var . "' ORDER BY Form_Date DESC";