Convert My sql query into active records query codeigniter - codeigniter

SELECT * FROM cdr WHERE (Circle LIKE '%D%' OR CLI LIKE '%D%' OR Operator LIKE '%D%') AND Dept = 'Sale'
I just want it like $this->db->like('Circle','%D%') with Dept with 'and'.

You should read the codeigniter doc, it's an easy one :
$this->db->where("(Circle LIKE '%D%' OR CLI LIKE '%D%' OR Operator LIKE '%D%')")
->where("dept", "Sale");
$query = $this->db->get("cdr");
http://www.codeigniter.com/user_guide/database/active_record.html

Related

How to order by heading and then the description

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');

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

Hive use variable from a query that return only one value

I would like to use something like:
set hivevar:average = select avg(nvl(val,0)) as average from table;
and Then use on another table like:
select * from table2 where value>=${average}
Is this possible in hive? if not how is the correct way on doing

Is there a Hive equivalent of SQL “LIKE ANY ( SUBQUERY )”

While Hive doesn't supports multi-value LIKE queries which are supported in SQL : ex.
SELECT * FROM user_table WHERE first_name LIKE ANY ( 'root~%' , 'user~%' );
We can convert it into equivalent HIVE queries as :
SELECT * FROM user_table WHERE first_name LIKE 'root~%' OR first_name LIKE 'user~%'
Does anyone know an equivalent solution that Hive does support in case sub-query is used with LIKE ? Have a look at below example :
SELECT * FROM user_table WHERE first_name LIKE ANY ( SELECT expr FROM exprTable);
As It doesn't have values in expression, I can't use same approach for generating multiple LIKE expression separated with OR / AND operator. Initially I thought to write HIVE UDF for it ? Can you please help me supporting such expression and finding HIVE equivalent ?
You can use Hive's RLIKE relational operator as shown below,
SELECT * FROM user_table WHERE first_name RLIKE 'root~|user~|admin~';
Hope this helps!
This is a case involving theta joins in Hive. There is a wiki page for this and a jira request. Please go through the details here on this page: https://cwiki.apache.org/confluence/display/Hive/Theta+Join
Your case is similar to the Side-Table Similarity case given on the page.
You need to convert the expr values into a map and then use regular expression to find the like. Alternatively you can also use union all with all the like expressions in separate SQL - the query might become tedious so you can programatically generate it.
What about this using EXISTS
SELECT * FROM user_table WHERE EXISTS ( SELECT * FROM exprTable WHERE first_name LIKE expr );

CodeIgniter ActiveRecord Query

I have the following code:
$this->db->from('addresses');
$this->db->where('user_id', $this->session->userdata('user.id'));
$this->db->like('country', $pSearch);
$this->db->or_like('state', $pSearch);
$this->db->or_like('city', $pSearch);
$this->db->or_like('zip', $pSearch);
which generates the following SQL
SELECT *
FROM (`addresses`)
WHERE `user_id` = '1'
AND `country` LIKE '%d%'
OR `state` LIKE '%d%'
OR `city` LIKE '%d%'
OR `zip` LIKE '%d%'
ORDER BY `country` asc
LIMIT 15
Is there a way to make it generate it like this:
SELECT *
FROM (`addresses`)
WHERE `user_id` = '1'
AND (`country` LIKE '%d%'
OR `state` LIKE '%d%'
OR `city` LIKE '%d%'
OR `zip` LIKE '%d%')
ORDER BY `country` asc
LIMIT 15
As i whant to get only the records for user_id = 1 and not for all users.
That seems to be a bug/limitation in CI ActiveRecord. You could try Ignited Query, which has the ability to handle nested WHERE clauses, and probably can handle the nested WHERE/LIKE the way you like.
Check out this thread also for more info.

Resources