codeigniter - Mysql: Invalid argument supplied for foreach() - mysql

function countSv_AV_total(){
$this->db->flush_cache();
$this->db->start_cache();
$sql = "select Verzekeringen.naam,\n"
. " count(*) total,\n"
. " sum(case when (Age <= 18) then 1 else 0 end) Age18,\n"
. " sum(case when (Age BETWEEN 19 AND 30) then 1 else 0 end) Age19,\n"
. " sum(case when (Age BETWEEN 31 AND 50) then 1 else 0 end) Age30,\n"
. " sum(case when (Age BETWEEN 51 AND 69) then 1 else 0 end) Age50,\n"
. " sum(case when (Age >= 70) then 1 else 0 end) Age70\n"
. " from customers\n"
. " INNER JOIN Verzekeringen\n"
. " ON customers.AV=Verzekeringen.verzekeringcode\n"
. " AND Label =1\n"
. " group by Verzekeringen.naam LIMIT 0, 30 ";
$this->db->query($sql);
echo $this->db->last_query().'<br />';
return $this->db->count_all_results('customers','Verzekeringen');
$this->db->flush_cache();
}
I'm using this SQL and in the Mysql workbench it's given the right output. Now I want this to get this to the codeigniter Table class.
I get an error while foreach is not recieving data:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/welcome_message.php
Line Number: 178
Can somebody provide an solution. Thanks in advance

Related

How to use a user-defined variable in MySQL stored procedure?

I am trying to use a CASE statement in MySQL based on a variable. I tried the code below; however, I have found that "The SELECT statement cannot refer to system variables or user-defined variables". What I am trying to achieve is that I have an input date. I get its day of week so I know which day it is on (1 is Sunday). I then only want rows returned in the query that have the day set to "Y" (e.g., if day of week is 1 then stt_sunday is "Y"). The table has:
stt_id
stt_start_date
stt_end_date
stt_start_time
stt_end_time
stt_sunday
stt_monday
stt_tuesday
stt_wednesday
stt_thursday
stt_friday
stt_saturday
My code is:
String selectQry1 = (" SET #clickedDateDOW = clickedDateDOW; " +
" SELECT st_type, csm_session_timetable.stt_id, stt_start_date, stt_end_date, stt_start_time, stt_end_time " +
" FROM csm_session_type, csm_session_timetable " +
" WHERE csm_session_type.cli_id = ? " +
" AND csm_session_type.prm_id = ? " +
" AND csm_session_type.st_id = csm_session_timetable.st_id " +
" AND (csm_session_timetable.stt_end_date IS NULL || csm_session_timetable.stt_end_date > ?) " +
" AND csm_session_timetable.gym_id = ? " +
" AND CASE " +
" WHEN #clickedDateDOW = 1 AND stt_sunday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 2 AND stt_monday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 3 AND stt_tuesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 4 AND stt_wednesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 5 AND stt_thursday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 6 AND stt_friday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 7 AND stt_saturday LIKE 'Y' THEN 1 " +
" ORDER BY stt_start_date DESC, stt_start_time, stt_end_date DESC;");

How to add SELECT TOP (CASE WHEN user=#{username} THEN 100 ELSE 0 END) PERCENT * FROM (SELECT .... )?

My goal is to return either all of the row or just an empty array [] if "not one row" have column user with value #{username}.
This is the SQL SELECT statement
#Select("SELECT * FROM ( SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} )")
List<MemberModel> getChannelDetail(String username, String channelUuid);
My goal is to change the 100 PERCENT to 0 PERCENT if "not one row" have column user with value of #{username}
What I've tried
Add SELECT TOP (CASE WHEN user=#{username} THEN 100 ELSE 0 END) PERCENT ...
#Select("SELECT * FROM ( SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} ) " +
"LIMIT CASE WHEN user=#{username} THEN ALL ELSE 0 END")
List<MemberModel> getChannelDetail(String username, String channelUuid);
But it throws an error
Column "USER" not found; SQL statement:

SELECT with JOIN And SUM in JPQL

I wanna do a query for extract information in a dashboard. I have two entities: User and Post, they are a manytomany relation. In my query, I wanna get the post information and the number of users by rol in each post.
I have tried this query, and it is working, but when there is a post without users, it isn't coming in the resultset, and it should be "| post1 | information | 0 | 0 |".
#Query("SELECT new com.project.dto.DashboardDTO("
+ "post.title, "
+ "post.information, "
+ "SUM(CASE WHEN u.rol = 0 THEN 1 ELSE 0 END), "
+ "SUM(CASE WHEN u.rol = 1 THEN 1 ELSE 0 END)) "
+ "FROM Post post JOIN post.users u "
+ "GROUP BY post.title, post.information")
Page<DashboardDTO> getDashboard(Pageable pageable);
What have I to fix?
You need to use left join instead of join, and you'll get the desired result.
Also if you need to get the summarization details for each post, you can group by the post id instead of (title, information) because both of them functionally depend on the id.
The query would be:
#Query("SELECT new com.project.dto.DashboardDTO("
+ "post.title, "
+ "post.information, "
+ "SUM(CASE WHEN u.rol = 0 THEN 1 ELSE 0 END), "
+ "SUM(CASE WHEN u.rol = 1 THEN 1 ELSE 0 END)) "
+ "FROM Post post LEFT JOIN post.users u "
+ "GROUP BY post")
Page<DashboardDTO> getDashboard(Pageable pageable);

How to remove the seperator if all values are NULL in MySQL CONCAT_WS?

I am trying to use CONCAT_WS to generate a subject line. However, I want to ignore the seperator if a value is null.
Please look at this query
SELECT
CONCAT_WS(" ",
CASE WHEN total_attempts > 0 THEN
CONCAT( call_code_title , " - ", total_attempts, "<sup>",
CASE WHEN total_attempts = 2 THEN "nd"
WHEN total_attempts = 3 THEN "rd"
ELSE "th" END
, "</sup> attempt") ELSE call_code_title END
, "-", program_name) AS callSubject
FROM table
The problem is when "program_name" IS NULL then I always will have "-" at the end of the string. I want to not concat "-" id program_name IS NULL
How do I do that?
Thanks
IFNULL(program_name, concat("-", program_name))
SELECT
CONCAT_WS(" ",
CASE WHEN total_attempts > 0 THEN
CONCAT( call_code_title , " - ", total_attempts, "<sup>",
CASE WHEN total_attempts = 2 THEN "nd"
WHEN total_attempts = 3 THEN "rd"
ELSE "th" END
, "</sup> attempt") ELSE call_code_title END
, IFNULL(program_name, concat("-", program_name)) , " " ) AS callSubject
FROM table
SELECT
CONCAT_WS(" ",
CASE WHEN total_attempts > 0 THEN
CONCAT( call_code_title , " - ", total_attempts, "<sup>",
CASE WHEN total_attempts = 2 THEN "nd"
WHEN total_attempts = 3 THEN "rd"
ELSE "th" END
, "</sup> attempt") ELSE call_code_title END
, CONCAT("-", program_name) ) AS callSubject
FROM table
If program_name is NULL, then CONCAT("-", program_name) will also return NULL, which CONCAT_WS will handle correctly by ignoring what it sees as a single null argument.
CONCAT() returns NULL if any argument is NULL.
— http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_concat

Query for choose all orders from all restaurants for the current day

How can I write a mysql query that will choose all the orders of all restaurants for the current day?
Some restaurants may close after 24:00.
So I have three tables:
restaurants with some fields [id, ]
orders with some fields [id, resid, orderdate, ]
hours with some fields [id, resid, day, open, close] the day is a number from 0-6, the open and close are time between 00:00:00 and 23:59:59
Thank you
This is what about i need
SELECT b.*
FROM restaurants a, orders b, hours c
WHERE a.id=b.resid
AND a.id=c.resid
---- here i need the code that will choose the orders for the current working day of the restaurant, but the current working day may include some orders from the previous calendar day if the restaurant close after 24:00
Is this what you want?
select o.*
from orders o
where o.orderdate = curdate();
If not, can you modify your question to better explain what you want?
Because you did not give much information, so I make 2 assumption here
Assumption 1: You run this query at 1AM and want to get all order your 1AM yesterday till now
Assumption 2: No restaurant open 24h day
hours.day = WEEKDAY(SUBDATE(CURDATE(), 1)
If hours.close < hours.open mean restaurant open from YESTERDAY + open_time to TODAY + close_time, otherwise it is open and close on YESTERDAY
STR_TO_DATE(CONCAT(DATE_FORMAT(SUBDATE(CURDATE(), CASE WHEN c.close < c.open THEN 1 ELSE 0 END), '%Y%m%d'), c.close), '%Y%m%d%T')
So your query is something like
SELECT b.*
FROM restaurants a, orders b
WHERE a.id=b.resid
AND a.id in (
select c.resid
from hours c
where c.day = WEEKDAY(SUBDATE(CURDATE(), 1)
and STR_TO_DATE(CONCAT(DATE_FORMAT(SUBDATE(CURDATE(), 1), '%Y%m%d'), c.open), '%Y%m%d%T') > SUBDATE(CURDATE(), 1)
and STR_TO_DATE(CONCAT(DATE_FORMAT(SUBDATE(CURDATE(), CASE WHEN c.close < c.open THEN 1 ELSE 0 END), '%Y%m%d'), c.close), '%Y%m%d%T') <= CURDATE()
)
I'm not sure about orders.orderdate, so you should update your filter on orders table to get correct data you want.
Hope this help
The solution is
" SELECT b.*".
" FROM orders b, rests a, times c ".
" WHERE a.id=b.resid ".
" AND a.id=c.resid ".
" AND (".
"( c.day=dayofweek(subdate(current_date, 1))-1 " .
" AND c.close < c.open " .
" AND c.close> TIME(NOW())" .
" AND b.orderdate>= CONCAT(subdate(current_date, 1),' ',c.open) )" .
" OR " .
"( c.day=dayofweek(current_date)-1 " .
" AND c.close >= c.open " .
" AND c.close> TIME(NOW())" .
" AND b.orderdate>= CONCAT(current_date,' ',c.open) )" .
" OR " .
"( c.day=dayofweek(current_date)-1 " .
" AND c.close < c.open " .
" AND CONCAT(ADDDATE(CURRENT_DATE, 1),' ',c.close)> TIME(NOW())" .
" AND b.orderdate>= CONCAT(current_date,' ',c.open) )" .
")";