How to order by heading and then the description - codeigniter

I have a table with heading and description when user searches the blog it should list first all the heading and then the description currently it is ordering by date created. And search should be case insenstive
$SQL = "(SELECT * FROM {$this->blogs} WHERE Lower(heading) LIKE '%" .strtolower($query) . "%' )
UNION
(SELECT * FROM {$this->blogs} WHERE Lower(description) LIKE '%" . strtolower($query) ."%')";
$run = $this->db->query( $SQL );
Query1
SELECT * FROM tbl_wonderblogs WHERE LOWER(heading) LIKE '%indian army amfc%' ) UNION (SELECT * FROM tbl_wonderblogs WHERE LOWER(description) LIKE '%indian army amfc%'
Query2
SELECT * FROM tbl_wonderblogs WHERE LOWER(heading) LIKE '%indian army afmc%' ) UNION (SELECT * FROM tbl_wonderblogs WHERE LOWER(description) LIKE '%indian army afmc%'
What is difference between query1 and query2 in which query 1 yeild results and query 2 does not yeild any results

The results are sorted by date created because of this code
$this->db->order_by('createdon DESC');
It asks that output is sorted by the contents of the createdon field in descending order.
I think you will find the following will make the output sort the way you want.
$this->db->order_by('description DESC, heading DESC');
There are a couple of different ways to use the order_by() method as shown in the documentation. The following will produce the same result as the previous code.
$this->db->order_by('description' 'DESC');
$this->db->order_by('heading', 'DESC');

Related

Is there an alternative to MySQL Field() function in VFP?

I have a multi fields table with a Countries column. I like the results from a query to be ordered by a particular country first and the rest alphabetically. In MySQL I would do something like:
Select * from myTable Order By Field(Countries,'Italy'),Countries
In Visual-FoxPro I have tried indexing the Cursor created by this query:
Select * from myTable Order By Countries
Index on Countries<>'Italy' TAG test
This would display all results for 'Italy' first, but leave the rest in an unpredictable order.
How to achieve this in Visual-FoxPro?
In VFP you can do it with something like this:
SELECT * FROM myTable WHERE Countries='Italy' ;
UNION ALL ;
SELECT * FROM (SELECT * FROM myTable WHERE Countries<>'Italy' ORDER BY Countries) as secsel
It does order by "if countries is not Italy first then Italy", Countries. Right?
In VFP you can use IIF(). ie:
Select *, iif(Countries == 'Italy', 1, 0) as CItaly ;
from myTable :
Order By CItaly,Countries
Note: If you want to do this via an index then you can use a composite index like:
index on iif(Countries = 'Italy', '1', '0') + Countries tag myCountry

Count all the grouped rows with Eloquent

I'm trying to count all the grouped rows but have no clue how to do it, if possible, with Eloquent.
My query looks like this:
SELECT COUNT(*) FROM (SELECT date FROM `reports` WHERE project_id = X GROUP BY `date`) as t1
As of now I'm using raw database queries:
DB::select(DB::raw("SELECT COUNT(*) as total FROM (SELECT date FROM `" . (new Report)->getTable() . "` WHERE project_id = " . ((int) $this->project->id) . " GROUP BY `date`) as t1"))[0]->total
Is it possible to achieve this with Eloquent, or at least make the PHP "call" prettier?
You can use the Query Builder:
I'd do something along this (I don't think you need that subquery):
Report::where('project_id', $this->project->id)
->count(DB::raw('DISTINCT date'));

Converting a raw query to Laravel query builder

I have the following MySQL query which fetches a list of the last 9 authors to write a post and lists them in order of the date of the last post they wrote.
It's working properly but I'd like to re-write it using the Laravel Query Builder. Here is the query at the moment:
$authors = DB::select("
SELECT
`a`.`id`,
`a`.`name`,
`a`.`avatar`,
`a`.`slug` AS `author_slug`,
`p`.`subheading`,
`p`.`title`,
`p`.`slug` AS `post_slug`,
`p`.`summary`,
`p`.`published_at`
FROM
`authors` AS `a`
JOIN
`posts` AS `p`
ON `p`.`id` =
(
SELECT `p2`.`id`
FROM `posts` AS `p2`
WHERE `p2`.`author_id` = `a`.`id`
ORDER BY `p2`.`published_at` DESC
LIMIT 1
)
WHERE
`a`.`online` = 1
ORDER BY
`published_at` DESC
LIMIT 9
");
I understand the basics of using the query builder, but there doesn't appear to be anything in the Laravel docs that allows for me to JOIN a table ON a SELECT.
Can anyone suggest a way that I can write this query using the Laravel Query builder, or perhaps suggest a way that I can rewrite this query to make it easier to structure with the query builder?
Try to do like this
$data = DB::table('authors')
->select(
'a.id',
'a.name',
'a.avatar',
'a.slug AS author_slug',
'p.subheading',
'p.title',
'p.slug AS post_slug',
'p.summary',
p.published_at')
->from('authors AS a')
->join('posts AS p', 'p.id', '=', DB::raw("
(
SELECT p2.id FROM posts AS p2
WHERE p2.author_id = b.id
ORDER BY p2.published_at
DESC LIMIT 1
)"))
->where('a.online', 1)
->limit(9)
->orderBy('p.published_at', 'desc')
->get();

SELECT * FROM (SELECT ..) in Eloquent (Laravel4)

How to do this query: SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id with Eloquent in Laravel4?
Thanks in advance.
I just looked it up but couldn't find a way to do it.
What you can is to just create a normal Eloquent query and then sort it by PHP.
$query = MyModel::orderBy('id', 'desc')->limit(3)->get();
$query->sortBy(function($object) {
return $object->id;
});
dd($query->toArray());
Raw: DB::select('SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id');
Why using Eloquent all the time? It has so many limitations. You have 3 options when querying a database. Eloquent, Query Builder and Raw.

How to query a list of several thousand IDs [duplicate]

This question already has an answer here:
Select IN on more than 2100 values
(1 answer)
Closed 8 years ago.
In Coldfusion I'm trying to do a select query on an Oracle database table that I only have read access to. I have a list (lets call it myList) of several thousand IDs that I want to do the query on.
SELECT * FROM table
WHERE userID IN (#myList#)
The issue I have is that Oracle only lets me use IN with 1000 items at a time. What is the most efficient way to approach this? Break up the list by 1000 and append the resulting queries? If so what should my code for breaking the list look like?
Thanks!
There is a function available on cflib.org called ListSplit. It splits your long list into an array of shorter lists. Once you find and run this, your query starts to look like this:
select JustTheFieldsYouNeed
from SomeTables
where 1 = 3
<cfloop array="#ArrayOfShorterLists#" index = "ThisList">
or SomeField in
(<cfqueryparam cfsqltype="cf_sql_integer" value = "#ThisList#" List = "yes"> )
</cfloop>
If you're looking to do this for ad hoc queries, this can be done with a WITH query:
WITH
UserList AS
(
SELECT 'UserID1' AS UserID FROM DUAL UNION ALL
SELECT 'UserID2' AS UserID FROM DUAL UNION ALL
...
SELECT 'UserIDX' AS UserID FROM DUAL
)
SELECT t.*
FROM table t
JOIN UserList U
ON t.UserID = U.UserID
That list in between the parentheses of the UserList WITH query can be as long as you want it to be, and can pretty easily be created with Excel (assuming that's where the list you want to check originated).
-
Example Excel formula (assuming your first value is in A2):
="SELECT '" & A2 & "' AS UserID FROM DUAL" & IF(A3="", "", " UNION ALL ")
or, if your UserID is numeric:
="SELECT " & A2 & " AS UserID FROM DUAL" & IF(A3="", "", " UNION ALL ")
(autofilling this down to the bottom of your list will create everything you need to paste in between the parentheses of that query above)
-
HOWEVER, note that this is only good for ad hoc type queries. This is not a good solution if you need to do this on an automatic basis. If this is for a more permanent solution, use temp (or permanent) tables.
Edit Starts Here
Here is some simple code that shows how to do this in ColdFusion
<cfset ids = "1,2">
<cfquery name="x" datasource="oraclesomething">
with IDList as (
select 0 id
from dual
where 1 = 2
<cfloop list="#ids#" index="ThisID">
union
select #ThisID# id
from dual
</cfloop>
)
select count(*) from some_table join IDList on someid = id
</cfquery>
<cfdump var="#x#" metainfo="no">

Resources