-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
If you want people to look at your code then format it so it is clear, by virtue of its indentations, where your various control blocks start and end. (BTW, our first if() {…} block has no end) Put your code in a code block, either with code tags or use the <> button in the toolbar.
-
Your time might have been better spent reading on the first night and fixing on the second. Sounds like you want us to do it for you rather than give help.
-
Presumably he posted legible, well formatted code in that one. Thanks for the heads up.
-
Then you definitely need a left join. Without it, if there is no data, there is no row. Consider a teacher taking the morning attendance roll call. There are two way they can do it use a register of all the names and check off each child present request that anyone not present raise their hand The former method is the LEFT JOIN approach; the latter method is what you are attempting which suggests that the 'enne' condition should also be in the join's ON clause... SELECT d.date , CASE WHEN enne IS NULL THEN 'NO DATA' ELSE SUM(score > 2) END as head FROM ( SELECT DISTINCT date FROM kilnakorr ) d LEFT JOIN kilnakorr k ON d.date = k.date AND enne = 'ennehead' AND user='fred' AND department='sales' GROUP BY d.date; EDIT: Note that creating the data table using a subquery assumes that there will be at least one record for every date for someone. If this is not the case then you need to generate a (temporary) date table containing all the dates you need to appear, as in my earlier example.
-
Then we'll use sql magic to conjure the date table with a subquery SELECT d.date , CASE WHEN enne IS NULL THEN 'NO DATA' ELSE SUM(CASE WHEN `enne`='ennehead' AND score>2 THEN 1 ELSE 0 END) END as head FROM ( SELECT DISTINCT date FROM kilnakorr ) d LEFT JOIN kilnakorr k ON d.date = k.date AND user='hercules' AND department='sales' GROUP BY d.Date; +------------+---------+ | date | head | +------------+---------+ | 2019-10-01 | NO DATA | | 2019-10-02 | NO DATA | | 2019-10-03 | NO DATA | +------------+---------+
-
OK, my final guess at you scenario INPUT... TABLE: kilnakorr_date TABLE: kilnakorr +------------+ +----+----------+------------+-------+-------+------------+ | date | | id | enne | date | score | user | department | +------------+ +----+----------+------------+-------+-------+------------+ | 2019-10-01 | | 13 | ennehead | 2019-10-01 | 1 | fred | sales | | 2019-10-02 |-----------+ | 14 | ennehead | 2019-10-01 | 3 | fred | sales | | 2019-10-03 | | | 15 | ennehead | 2019-10-01 | 5 | fred | sales | +------------+ | | 16 | head | 2019-10-01 | 1 | fred | sales | | | 17 | ennehead | 2019-10-01 | 3 | curly | accts | +------------0<| 18 | ennehead | 2019-10-01 | 5 | mo | accts | | 19 | ennehead | 2019-10-02 | 1 | fred | sales | | 20 | ennehead | 2019-10-02 | 3 | pete | sales | | 21 | ennehead | 2019-10-02 | 5 | mary | sales | | 22 | head | 2019-10-02 | 1 | fred | sales | | 23 | ennehead | 2019-10-02 | 3 | curly | accts | | 24 | ennehead | 2019-10-02 | 5 | mo | accts | +----+----------+------------+-------+-------+------------+ Then... SELECT d.date , CASE WHEN enne IS NULL THEN 'NO DATA' ELSE SUM(CASE WHEN `enne`='ennehead' AND score>2 AND user='fred' THEN 1 ELSE 0 END) END as head FROM kilnakorr_date d LEFT JOIN kilnakorr k ON d.date = k.date AND score>2 AND department='sales' GROUP BY d.Date; +------------+---------+ | date | head | +------------+---------+ | 2019-10-01 | 2 | | 2019-10-02 | 0 | | 2019-10-03 | NO DATA | +------------+---------+
-
(PHP) Need to change currency function to percentual (%) (at one line)
Barand replied to Netchain's topic in PHP Coding Help
Have you tried echo $change . '%'; -
Not sure I totally understand. If you had this situation below, what output would you want to see? SELECT * FROM kilnakorr; +----+------+------+------+------+ | id | cola | colb | colc | cold | +----+------+------+------+------+ | 1 | bar | foo | bar | NULL | | 2 | foo | foo | NULL | NULL | | 3 | foo | foo | bar | NULL | | 4 | bar | foo | NULL | NULL | +----+------+------+------+------+ SELECT SUM(CASE WHEN cola = 'bar' THEN 1 ELSE 0 END) as tota , SUM(CASE WHEN colb = 'bar' THEN 1 ELSE 0 END) as totb , SUM(CASE WHEN colc = 'bar' THEN 1 ELSE 0 END) as totc , SUM(CASE WHEN cold = 'bar' THEN 1 ELSE 0 END) as totd FROM kilnakorr; +------+------+------+------+ | tota | totb | totc | totd | +------+------+------+------+ | 2 | 0 | 2 | 0 | (What do you want to see here?) +------+------+------+------+
-
(PHP) Need to change currency function to percentual (%) (at one line)
Barand replied to Netchain's topic in PHP Coding Help
percent = (amount_changed / yesterdays_closing_price) * 100 -
$price = 278.53; $cents = $price * 100; $end_result = $cents ; // without the (int) gives 27853
-
foreach ($data as $k => $v) { if (is_object($v)) { foreach ($v as $k1 => $v1) { echo "$k1 : $v1 <br>"; } } }
-
That "array" is an object. (if it came from json data, decode it as an array instead of an object) $a = json_decode(json_encode($data), true); // convert the object to an array foreach ($a as $k => $v) { if (is_array($v)) { echo $v['orders.orderid'] . '<br>'; } }
-
Getting the error Undefined index but the column exists
Barand replied to makamo66's topic in PHP Coding Help
So you are one of those people who likes to waste people's time by posting simultaneously on multiple forums? That will be remembered. -
Getting the error Undefined index but the column exists
Barand replied to makamo66's topic in PHP Coding Help
Your error is because you have no parameter placeholders in the query that you are preparing. (See the examples) Dates stored in that format are as much use as a chocolate teapot. You cannot do correct comparisons and therefore you can't sort them. You can't use the dozens of date/time functions without reformatting Store data for functionality, not prettiness. You can format it for human consumption on output, I've told you the correct format to use. -
Isn't that just another category (I.E. Free)?
-
Other than filtering by category, how is this different from your last topic?
-
Getting the error Undefined index but the column exists
Barand replied to makamo66's topic in PHP Coding Help
I see no problem with this (eg if date comes from a date picker) as the date finally used is not that originally input... $date = (new DateTime($_POST['date']))->format('Y-m-d'); $res = $pdo->query("SELECT whatever FROM tablename WHERE thedate > '$date' "); -
Getting the error Undefined index but the column exists
Barand replied to makamo66's topic in PHP Coding Help
String literals in a query require single quotes otherwise they are treated as column names. Identifiers which contain spaces, or are reserved words, require backticks. SELECT `group` , fname as `first name` FROM user WHERE lname = 'jones' ; -
Getting the error Undefined index but the column exists
Barand replied to makamo66's topic in PHP Coding Help
DATETIME columns require Y-m-d H:i:s format. (eg 2019-10-23 14:52:38 ) Have you checked the column exists, say, with "DESCRIBE comment_table" or "SHOW CREATE TABLE comment_table"? @gw1500se the quotes are correct mysqli_query($connection, "INSERT INTO comment_table (name, date_col) VALUES ('$name', '$date_col')"); ^ ^ -
Perhaps exec * 100 / total
-
As an easier alternative to SELECT TIME_FORMAT(IF(logged=0, time, logged), '%T') as time Then you will just need SELECT TIME_FORMAT(logged, '%T') as time
-
This will copy the current date and time values into the timestamp column UPDATE tblTraffic SET logged = CONCAT(date, ' ', time);
-
All existing records will have the timestamp auto updated to the time the column was added. When new records are added that timestamp column will be the time added. You could set that column to zero date (for currently existing records) so only new records get a time stamp. UPDATE tblTraffic SET logged = 0; Then you will know which time to use - the timestamp if non-zero or the time column if timestamp is zero. SELECT TIME_FORMAT(IF(logged=0, time, logged), '%T') as time EDIT: Alternatively, copy the date and time columns' values into the timestamp column
-
Try this... Add a TIMESTAMP type column to your table `logged` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, then use the time portion of this column in your query instead of your current time column. (You don't need to change the INSERT query as it will update itself automatically) $sql = mysqli_query($conn, "SELECT ip , page , CASE WHEN referrer = '' THEN 'N/A' ELSE referrer END as referrer , DATE_FORMAT(date, '%m/%d/%y') as date , TIME_FORMAT(logged, '%T') as time FROM tblTraffic ORDER BY date DESC, time DESC");