CodeIgniter ActiveRecord Query - codeigniter

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.

Related

Oracle PL/SQL How to Get All Rows When the Date Parameter is Null?

This is where statement of my query:
WHERE date_column LIKE
(CASE
WHEN :date_parameter IS NOT NULL
THEN
:date_parameter
ELSE '%%'
END)
...
If the parameter is null, I want to get all rows. How can I do this?
Try
WHERE (:date_parameter is null
OR date_column = :date_parameter)
I used to write
WHERE date_column = nvl(:date_parameter,date_column)
But I think people find it confusing to read.
This should be work:
WHERE (date_column = :date_parameter AND :date_parameter IS NOT NULL) OR :date_parameter IS NULL

Convert My sql query into active records query 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

Oracle unions in the wrong order

I'm trying to understand why my query is returning things in a different order than I expect.
My query is:
SELECT 'PRINTJOBID', MAX(PRINTJOBID), null, null FROM PRINTJOB
UNION
SELECT 'AUTOID', null, MAX(AUTOID), null FROM PRINTJOBSHELLS
UNION
SELECT 'PROCESSLOGID', null, null, MAX(PROCESSLOGID) FROM PROCESSLOG;
I am expecting it to give me 3 rows, with printjobid at the top, followed by autoid, and then at the bottom should be processlogid. However, when I run the query I get autoid at the top, printjobid in the middle, and processlog at the bottom, like this:
AUTOID null 771426 null
PRINTJOBID 76401 null, null
PROCESSLOGID null null 1218693
I have tried googling about Unions being in the wrong order, and I tried searching questions on SO. I didn't see anything that seemed relevant. Is my understanding of how UNION works faulty? I thought that the query would return the rows in the order I put the select statements. Thank you!
In SQL, you can't rely on a query returning results in any special order unless you explicitly specify it in the query.
Use this:
SELECT title, v1, v2, v3
FROM (
SELECT 'PRINTJOBID' title, MAX(PRINTJOBID) v1, null v2, null v3, 1 AS o
FROM PRINTJOB
UNION ALL
SELECT 'AUTOID' title, null, MAX(AUTOID), null, 2 AS o
FROM PRINTJOBSHELLS
UNION ALL
SELECT 'PROCESSLOGID' title, null, null, MAX(PROCESSLOGID), 3 AS o
FROM PROCESSLOG
)
ORDER BY
o
If you want ordering of results, you need to specify an ORDER BY clause. Otherwise you are relying on implementation-specific behaviour. In this case it is probably lucky you did not get the ordering you hoped for originally, as the behaviour could easily change in future, so you need to explicitly specify ordering if it is important to you.

Oracle dba_tab_cols query

Hi is it possible to retrieve the primary key and unique key using the dba_tab_cols query?
Is there any query that allows me to retrieve all of the following fields?
Column Name
Data Type
Primary Key
Null/Not Null
Unique Key
Default Value
Extra
Both primary and unique keys can span more than one column, so they wouldn't belong in dba_tab_columns. You'd need to look at dba_constraints and dba_cons_columns to get that information.
This is a starting point, maybe:
select owner, table_name, column_name, data_type, primary_key,
nullable, unique_key, data_default
from (
select dtc.owner, dtc.table_name, dtc.column_id, dtc.column_name,
dtc.data_type, dtc.nullable, dtc.data_default,
case when dc.constraint_type = 'P' and dcc.column_name = dtc.column_name
then dc.constraint_name end as primary_key,
case when dc.constraint_type = 'U' and dcc.column_name = dtc.column_name
then dc.constraint_name end as unique_key,
row_number() over (partition by dtc.owner, dtc.table_name, dtc.column_id
order by null) as rn
from dba_tab_columns dtc
left join dba_constraints dc
on dc.owner = dtc.owner
and dc.table_name = dtc.table_name
and dc.constraint_type in ('P', 'U')
left join dba_cons_columns dcc
on dcc.owner = dc.owner
and dcc.constraint_name = dc.constraint_name
and dcc.table_name = dc.table_name
and dcc.column_name = dtc.column_name
where dtc.owner = '<owner>'
and dtc.table_name = '<table_name>'
)
where rn = 1
order by owner, table_name, column_id;
I've done this with a subquery that generates a row_number value because you'd get duplicates for a table with more than one constraint; and because you want the default value, which is a long (column data_default), you can't use distinct or group by. It feels a bit inelegant, but I'm sure you can work on it to get what you need.
It's also possible to have a check constraint that replicates the not null version, though it isn't advisable. And a unique index won't show up as a unique constraint, so you might want to look for one of those too, via dba_indexes and dba_ind_columns. An index used to back up a unique constrain will appear in both, though.
You could look at dbms_metadata.get_ddl to get this information too, depending on what you intend to do with it. I'm not sure why this would be useful, other than to try to recreate the schema elsewhere, and there are better tools for doing that.

how to get alphabetically next and prev records wiht minimal fetched records?

I have a page that is displaying a company name and its details from a table A.
Now say i have a company displayed,and its name is 'Company_one' now i want to have alphabetically sorted next company and previous company and their details etc.
The data in my table is not sorted.Its stored as it gets the data.
So now what kind of query should i write that it gives only one previous and one next alphabetically sorted record??
Plz help!!
There's no nice way to do that in a single query. Just do two queries.
To get the previous one:
SELECT * FROM companies
WHERE name < variable_with_current_name
ORDER BY name DESC
LIMIT 1
To get the next one along:
SELECT * FROM companies
WHERE name > variable_with_current_name
ORDER BY name ASC
LIMIT 1
You need to use the sort clause to sort your table. The prototype for sort is:
sort by fieldname
Example Query:
select * from your_table sort by company asc
If you want to limit records, use limit clause:
select * from your_table sort by company asc limit 0, 1
Based on Dominic's answer, you can achieve the same result using a single query by combining them with WHERE and OR.
For example:
SELECT * FROM `companies`
WHERE (
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` < 'variable_with_current_name'
ORDER BY `name` DESC
LIMIT 1)
, 0)
OR
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` > 'variable_with_current_name'
ORDER BY `name` ASC
LIMIT 1)
, 0)
)
Hope that helps.

Resources