Jump to content

raw complex query


wtricks

Recommended Posts

I need to run this query:

SELECT * FROM `tblticketreplies` where tid IN (SELECT id FROM `tbltickets`  WHERE userid = '0' and date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND id in (SELECT max(id) FROM `tblticketreplies` GROUP BY tid ) and DATE(date) > DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)) order by id desc

but I'm having problems, since I can't seem to make it work using raw (not really experienced with laravel query builder.

I tried with

$replies = Capsule::raw(SELECT * FROM `tblticketreplies` where tid IN (SELECT id FROM `tbltickets`  WHERE userid = '0' and date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND id in (SELECT max(id) FROM `tblticketreplies` GROUP BY tid ) and DATE(date) > DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)) order by id desc);

But no luck. do you know how to do this?

Link to comment
Share on other sites

for something like that, you shouldn't need to use RAW (though you could if you wanted to)...

$replies = Capsule::select("SELECT * FROM `tblticketreplies` where tid IN (SELECT id FROM `tbltickets`  WHERE userid = '0' and date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND id in (SELECT max(id) FROM `tblticketreplies` GROUP BY tid ) and DATE(date) > DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)) order by id desc");

untested, but the above query should work and return an array of results - assuming there are any results that match the SQL query.

Link to comment
Share on other sites

thanks brian, it worked perfectly.

for the records I tried (only difference is raw instead of select):

$replies = Capsule::raw("SELECT * FROM `tblticketreplies` where tid IN (SELECT id FROM `tbltickets`  WHERE userid = '0' and date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND id in (SELECT max(id) FROM `tblticketreplies` GROUP BY tid ) and DATE(date) > DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)) order by id desc");

and it doesn't work (I don't need it, this is ok already

Link to comment
Share on other sites

when I said you could use raw, I meant that you could do....

$replies = Capsule::select(Capsule::raw("SELECT * FROM `tblticketreplies` where tid IN (SELECT id FROM `tbltickets`  WHERE userid = '0' and date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND id in (SELECT max(id) FROM `tblticketreplies` GROUP BY tid ) and DATE(date) > DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)) order by id desc"));

but that capsule::raw is not necessary...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated