I have below code to get the customers whose term is ending today.
$customers = CompanyModel::where(("term_ending") ,"=", "CURDATE()")->get();
But when I run this it returns null.
Same query I run in Mysql it gives me results.
select * from `companies` where `term_ending` = CURDATE()
What is wrong I am doing?
I solved it using Carbon.
$customers = CompanyModel::where(("term_ending") ,"=", Carbon::today())->get();
Thanks :)
Related
I have been plugging away at this and I am really at a loss for what I'm messing up. I am using My SQL workbench and sending queries to a mariadb. The original function I am working with, returns records just fine, however they pull in all of them (of which there are 1000+). Ideally the function would only return new records instead of all of them (hence no delete). I am trying to use WHERE NOT EXISTS however SQL is returning error code 1064. Sample code below:
SELECT WHERE NOT EXISTS
usr1.email
, case when ccm.course = 7 then 'CourseComplete'
when ccm.course = 10 then 'Course1' when ccm.course = 4 then 'Course1CourseComplete' else 'other' end as coursecompleted
, course
, date_add(from_unixtime(timecompleted), INTERVAL 1 HOUR) as CompletionDate
FROM moodledb.m_course_completions ccm
inner join m_user usr1
on ccm.userid = usr1.id
where timecompleted is not null and ccm.course in (4,7,10) order by 4 desc
In addition I've tried alternate placements for the where not exists function, but they all return the same error1064. Would a different operator work better? Can goku defeat this new foe? Any suggestions will be greatly appreciated
I am finding inputted user_id in accepted_join_id above query gives error as
syntax error, unexpected '$user_id' (T_VARIABLE)
$query=DB::select('select activity_id,accepted_join_id from table_user_create_activity WHERE FIND_IN_SET('$user_id',?) and activity_id=?',[$user_id,$accepted_join_id]);
if you want to inject $user_id value from php into your query try this:
$query=DB::select('select activity_id,accepted_join_id from table_user_create_activity WHERE FIND_IN_SET('.$user_id.',?) and activity_id=?',[$user_id,$accepted_join_id]);
or if $user_id is in query it should be like:
$query=DB::select('select activity_id,accepted_join_id from table_user_create_activity WHERE FIND_IN_SET("$user_id",?) and activity_id=?',[$user_id,$accepted_join_id]);
I've got a task to fetch a set of entities for a specific period of time.
I'm using code first approach and MySql server.
With the code
IQueryable<Entity> query = dbContext.Set<Entity>().AsNoTracking();
Entity[] entities = (from e in query
let last = (from ee in query orderby ee.Id descending select ee).FirstOrDefault()
where e.Timestamp >= DbFunctions.AddHours(last.Timestamp, -hours)
select e).ToArray();
And the following SQL generated by EF 6.1.3
SELECT
`Extent1`.`Id`,
`Extent1`.`Timestamp`
FROM `Entities` AS `Extent1` LEFT OUTER JOIN (SELECT
`Extent2`.`Id`,
`Extent2`.`Timestamp`
FROM `Entities` AS `Extent2`
ORDER BY
`Extent2`.`Id` DESC LIMIT 1) AS `Limit1` ON 1 = 1
WHERE `Extent1`.`Timestamp` >= (AddHours(`Limit1`.`Timestamp`, -(#p__linq__0)))
I face an error
Failed in 6 ms with error: FUNCTION mydb.AddHours does not exist
And indeed I haven't got that function at DB side.
Does anybody know how those DbFunctions work?
UPD
Similar questions
EntityFramework 6.1.3 and MySQL DbFunctions.TruncateTime does not exist?
how to use canonical functions in Entity Framework and Mysql
A workaround for now - just added an implementation of the function in DbInitializer
CREATE FUNCTION AddHours(timeValue datetime, addValue int) RETURNS datetime
RETURN DATE_ADD(timeValue, INTERVAL addValue HOUR);
You are trying to call a stored function of your database, but that does not exist. The solution is to properly create the AddHours db function. More information can be found here.
I'm trying to run a MySQL query via WordPress, to bring back a list of posts that I want to delete because they have no "like" votes (using someone else's plugin data). The query works perfectly in phpMyAdmin but gives a syntax error when I run it through WP... and I see absolutely no reason why it would do this.
Here's the query code, which checks for posts over 30 days old that have no corresponding "like" entry in wti_like_post (whether positive or negative):
$novotesquery = "SELECT * FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND $wpdb->posts.ID NOT IN
(SELECT DISTINCT post_id FROM $wpdb->wti_like_post)" ;
$result = $wpdb->get_results($novotesquery);
The syntax error says there's a problem on the last line of the SQL (the SELECT in parentheses): "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6".
When I run the query in phpMyAdmin (replacing "$wpdb->" with the table prefix), it works a treat. If anyone can tell me why the SQL query will run on the server and not in WP, I'd appreciate it.
Thanks in advance.
Perhaps it's just a matter of defensive parenthesis
$novotesquery = "SELECT * FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = 'post'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND {$wpdb->posts}.ID NOT IN
(SELECT DISTINCT post_id FROM {$wpdb->wti_like_post})" ;
Perhaps you should use $wpdb->query method instead of get_results and finally, perhaps wti_like_posts is not yet declared when your code runs.
What about die($novotesquery) right before "$result" line?
I have the following query in HQL:
update ProjectFile pf1
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf
where pf.validFrom.sequence <= pv.sequence
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)
Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:
You can't specify target table 'ProjectFile' for update in FROM clause
The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.
I bumped into the same problem and posted a question here: MySQL/SQL: Update with correlated subquery from the updated table itself.
To solve your problem, you need to join at the UPDATE level, please take a look at the answer to my question.