Codeigniter Query Builder join table with LIKE condition instead of where? - codeigniter-3

I can't find any answer in the documentation, I'm trying to achieve this:
$this-db->join("table", "column LIKE %string%");
But it's not working...

$this->db->join('table', 'table.a = table2.b');
$this->db->like('field_name_in_table','value_to_be_compared');
try this.
Note: Here table2 is base table.

Related

Postgresql json like query

I have the following table called module_data. Currently it has three rows of entries:
id data
0ab5203b-9157-4934-8aba-1512afb0abd0 {"title":"Board of Supervisors Meeting","id":"1i3Ytw1mw98"}
7ee33a18-63da-4432-8967-bde5a44347a0 {"title":"Board of Supervisors Meeting","id":"4-dNAg2mn6o"}
8d71ca35-74eb-4751-b635-114bf04843f1 {"title":"COPD 101", "id":"l9O0jCR-sxg"}
Column data's datatype is jsonb. I'm trying to query it using like operator. Something like the following:
SELECT * FROM module_data WHERE title LIKE '%Board%';
I've been looking at the jsonb support and there doesn't seem to be a like operator. If anyone has any advice.
If the data column is text type, then use ->> on cast:
select * from module_data where data::json->>'title' like '%Board%'
If it's already json:
select * from module_data where data->>'title' like '%Board%'
I found the following is more straight-forward and easier for jsonb type of columns:
select * from table_name
where
column_name::text like '%Something%'
Found a good article on more examples and implementations:
https://www.compose.com/articles/faster-operations-with-the-jsonb-data-type-in-postgresql/
Hope it helps!
One other option which may be sufficient for other people who've found this page is to just cast the column to text type. Eg
select * from module_data where data::text like '%Board%'
Note though, this will search over the entire json and should only be used if you can guarantee the other fields won't be a problem.
I Think it should be like
select * from module_data where data->>'$."title"' like '%Board%'
then only it worked for me.

Squeryl select top

I would like to do something like the following but using Squeryl:
select top 10 *
from table
where conditionA = a
so far I can only get to:
table.where(x => x.conditionA = a).head
The problem with this is the db call gets all the records that meets the condition from the db, while I only need the top one.
I cannot find another way to do the select top in Squeryl which only brings the necessary amount of records back from db.
Anyone know how to do this?
Thanks.
Squeryl has a method called page which you can use to specify a LIMIT and OFFSET for the query.
In your example; table.where(x => x.conditionA === a).page(0, 10).toList should achieve what you are looking to do.

LibreOffice Base - query - merge two rows

I'm learning LibreOffice Base (3.6.2). Unfortunatly the doc is pretty poor. The DB is a ".odb" file format. Here's a simple multi-table query:
I'd like to merge the field "refLogiciel.name" and "tblPosteLogiciel.version" in one field.
Thank you!
You will have to Create the query in SQL for that one. I have the same problem. No answers yet, but I do at least have some headway in that area. Here's a link to my question, which might help:
Error in Querying Concatenation of 2 fields in LibreOffice Base SQL
EDIT: Oops, I din't realize how old this post was. Hope you found out how to do it so you can share it to me. :)
use as field: CONCAT( "refLogiciel"."name" , "tblPosteLogiciel"."version")
If you want a space between the two then use:
CONCAT( "refLogiciel"."name" , CONCAT( ' ', "tblPosteLogiciel"."version"))

SQLAlchemy and Postgresql: to_tsquery()

Does anyone know how to use to_tsquery() function of postgresql in sqlalchemy? I searched a lot in Google, I didn't find anything that I can understand. Please help.
I hope it is available in filter function like this:
session.query(TableName).filter(Table.column_name.to_tsquery(search_string)).all()
The expected SQL for the above query is something like this:
Select column_name
from table_name t
where t.column_name ## to_tsquery(:search_string)
The .op() method allows you to generate SQL for arbitrary operators.
session.query(TableName).filter(
Table.c.column_name.op('##')(to_tsquery(search_string))
).all()
For these type of arbitrary queries, you can embed the sql directly into your query:
session.query(TableName).\
filter("t.column_name ## to_tsquery(:search_string)").\
params(search_string=search_string).all()
You should also be able to parameterize t.column_name, but can't see the docs for that just now.
This may have been added recently but worth adding as a more standard solution.
query.filter(Model.attribute.match('your search string'))
does it for you as it looks for the right operation available for your dialect.
See the official docs:
https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#full-text-search
Of course, this assumes the table you are querying is a view built with a to_tsvector attribute to apply the ## operation to.
My fifty cents in 2021, following the docs
None of the previous answers mention how to cast the text column as postgres: to_tsvector('english', column). I have a text column indexed as tsvector. And this is the way:
select(mytable.c.id).where(
func.to_tsvector('english', mytable.c.title )\
.match('somestring', postgresql_regconfig='english')
)
In my case, I didn't want to use to_tsquery as ".match" forces. A more intuitive way is to use websearch_to_tsquery when you have a search input like stackOverflow. So I did a mix from jd response.
I finally applied it as a .filter() the following statement:
func.to_tsvector('english', Table.column_name)\
.op('##')(func.websearch_to_tsquery("string to search",
postgresql_regconfig='english'))
I think this is the general formula and it applies to to_tsquery too.

ZF_DB: how to make simple select for two or more tables without join?

Example:
SELECT `cat`.`id_catalog`, COUNT(parent.id_catalog) - 1) AS `level` FROM `tbl_catalog` AS `cat`, `tbl_catalog` AS `parent` WHERE (cat.`left` BETWEEN parent.`left` AND parent.`right`) GROUP BY `cat`.`id_catalog` ORDER BY `cat`.`left` ASC
It doesn't seem to work if it use ZF. ZF create this query with join only. How to create the select without join in ZF_DB.
By the way may be I do smth wrong in this query. It is simple nested set DB with parent, left and right fields. Perhaps there are another way to use join to get deep for some node. Anyway it would be interesting to get answer for both way:)
Thanks in advance to all who looks it through:)