New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grouping LIKE clause (putting parenthesis around) #1881
Comments
In the development version, there are methods to do just that: http://codeigniter.com/nightly_user_guide/database/query_builder.html#query-grouping |
Thanks - I wasn't aware of that option available but It seems like it's for WHERE clause only? I tried group_start() around LIKE clauses but it failed. $this->db->group_start();
$this->db->like('name', $query_array['q']);
$this->db->or_like('description', $query_array['q']);
$this->db->or_like('others', $query_array['q']);
$this->db->group_end(); |
If you are using a 2.x version of CodeIgniter, I don't think it exists yet. |
I should have mentioned it - I am using 3.0-dev version - The above code works i.e. it adds the parenthesis etc but it's for WHERE clause only ... e.g. when I started the group around LIKE clause (above) it generated query like SELECT `id` , `name` , `description`
FROM `myfiles`
WHERE `is_public` =0
AND (
)
AND
`name` LIKE '%m%'
OR `description` LIKE '%m%'
OR `others` LIKE '%m%' so you can see it worked and added AND () but didn't group LIKE clause |
On 3.0-dev version I am trying this:
And here is my result:
|
Yes, I know... it doesn't group the like clause at the moment so I am using $this->db->query() to run my query... but it'll be good to know if this feature is planned or not ... |
Check out the feature/db_qb_aliasing branch and see if it works there. |
I have just tested the feature feature/db_qb_aliasing (EllisLab-CodeIgniter-v2.1.0-3078-gf83c436.zip). Code:
And this is the result:
It works there. |
Will feature/db_qb_aliasing get merged into version 3? |
Yes it will, it's actually one of the things blocking the 3.0 release that need to be finished (tested, at this point). |
I think about a suggestion, but I hesitate to open a new ticket. Let us reduce our example:
This way I can access a single value using one statement only. By adding a new method within the class CI_DB_result, this case may be written in a simpler way:
Here I am interested to get the first value of the row, this happens often. What do you think? Is it worthwhile? |
@ivantcholakov Doesn't this work?
I haven't tested, but by default both |
The value COUNT(id) needs an alias in your example, it works:
Let us see possible examples without aliasing on MySQL:
I am not sure whether other types of database servers would pick the column name in exactly the same way. Here is an intentionally wrong example, someone might get trapped by a similar one:
The suggested way releases the developer to think about the details above while doing his job, and I think it gives some visual comfort:
|
What you're proposing is a method depending on a query selecting a single value from a single row - that's rarely the case and using an alias is way more easier. |
OK. I don't insist on this. |
Merged. |
Try this I use "AND id != 0" just to complete the rule of the active query WHERE. It can be replaced freely as long as not interfere with the results of the query. Sorry, my English is bad. |
The above code produces this query ...
But I need it to be like this - Please note the parenthesis around LIKE clauses
Is there any way to tell CI to put thos parenthesis? I know there is
$this->db->query()
but would wait to see if this is possible with above code.The text was updated successfully, but these errors were encountered: