How to count for a substring over two tables in codeigniter - codeigniter

I have two tables
table1
id|name|next_field
table2
id|id_of_table1|whatelse
I do a msql query to get all entries of table1 and the number of entries in table2 who has table2.id_of_table1 = table1.id
This is my query - it works fine.
$select =array('table1.*', 'COUNT(table2.id) AS `my_count_result`',);
$this->db->select($select);
if($id!=false){ $this->db->where('id',$id); }
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't1.id = t2.id_of_table1');
return $this->db->get()->result_array();
Now I have another field which has coma-separated data
table1.next_field = info1, info2, next,...
Now I want to check in the same way like the first query how often for example "info2" is as a part inside the table1.next_field
Is it possible, how?

After all i decide to change my database structure to make the work and handle much easier.

Related

Consecutive JOIN and aliases: order of execution

I am trying to use FULLTEXT search as a preliminary filter before fetching data from another table. Consecutive JOINs follow to further refine the query and to mix-and-match rows (in reality there are up to 6 JOINs of the main table).
The first "filter" returns the IDs of the rows that are useful, so after joining I have a subset to continue with. My issue is performance, however, and my lack of understanding of how the SQL query is executed in SQLite.
SELECT *
FROM mytbl AS t1
JOIN
(SELECT someid
FROM myftstbl
WHERE
myftstbl MATCH 'MATCHME') AS prior
ON
t1.someid = prior.someid
AND t1.othercol = 'somevalue'
JOIN mytbl AS t2
ON
t2.someid = prior.someid
/* Or is this faster? t2.someid = t1.someid */
My thought process for the query above is that first, we retrieve the matched IDs from the myftstbl table and use those to JOIN on the main table t1 to get a sub-selection. Then we again JOIN a duplicate of the main table as t2. The part that I am unsure of is which approach would be faster: using the IDs from the matches, or from t2?
In other words: when I refer to t1.someid inside the second JOIN, does that contain only the someids after the first JOIN (so only those at the intersection of prior and those for which t1.othercol = 'somevalue) OR does it contain all the original someids of the whole original table?
You can assume that all columns are indexed. In fact, when I use one or the other approach, I find with EXPLAIN QUERY PLAN that different indices are being used for each query. So there must be a difference between the two.
The query should be simplified to
SELECT *
FROM mytbl AS t1
JOIN myftstbl USING (someid) -- or ON t1.someid = myftstbl.someid
JOIN mytbl AS t2 USING (someid) -- or ON t1.someid = t2.someid
WHERE myftstbl.{???} MATCH 'MATCHME' -- replace {???} with correct column name
AND t1.othercol = 'somevalue'
PS. The query logic is not clear for me, so it is saved as-is.

Spoon lookup obtaining two rows instead of duplicate columns

I have this two tables
If I use the lookup step using the EMPLOYEEID and YEAR I get this columns:
But I need to get this instead:
I have prepare a solution Here.
Using this you will get your result.
Please let me know if its ok with you.
A lookup is designed to retrieve fields from a matching record in another table and append them to the "master" record.
If you want to combine records from 2 tables into a single dataset then you need to UNION them together.
Update following comment
Something like this pseudo-code ought to work:
SELECT T1.*
FROM TABLE1 T1
UNION
SELECT T2.*
FROM TABLE1 T2
INNER JOIN TABLE1 T1A ON T2.KEY1 = T1A.KEY1 AND T2.KEY2 = T1A.KEY2 AND ...

Which of the following is most efficient one and why?

Following is the query which retrives all the order rows from the orders table
$orders = auth()->user()->orders;
or
$orders = Order::where('user_id',auth()->id())->get();
or
$orders = \DB::table('orders')->where('user_id',Auth::id())->get();
If orders table 100 000 rows then which of the above method is ideal to use?
Actually in Laravel you can use toSql method to compare the queries and see which one is the most optimized query like this:
$sql=auth()->user()->orders()->toSql();
But in the example above we see generally all of them are the same:
//First
"select * from `orders` where `orders`.`user_id` = ? and `orders`.`user_id` is not null"
//Second
"select * from `orders` where `user_id` = ?"
//Third
"select * from `orders` where `user_id` = ?"
As you see the second one and the third one are totally the same but the first one has an extra and statement and it's more reliable so I think the execution times are not different and you'd better to use the first one.
compare to laravel eloquent DB:: class is faster way to retrieve data from database but in our case we manage a application in structure way and its depend on your coding standard and module management.
i suggest to use :
$sql=auth()->user()->orders()->toSql();

Symfony3 multiple update with subquery

Is there ability to make a multiple update with subquery on Symfony3 with Doctrine query builder or DQL?
For example, I want to run this query:
UPDATE tableA
SET fieldA2 = max_field2
FROM (SELECT
field1,
max(field2) AS max_field2
FROM table
GROUP BY field1) AS subquery
WHERE subquery.field1 = tableA.field1;
I can't understand how to use $entityManager->createQuery()->update with FROM subquery.
As far as I know, it's not possible through DQL.
You need to go through a loop.
foreach($entities as entity)
{
$em->flush();
}
Else, you will consider Batch processing, or just use plain SQL. So it might be usefull to check DBAL.

Compare first row to other rows

The issue I face that I need after selecting multiple rows,to loop over each row and fetch those who have some related information to the first row.
Example:
select NAME,ENGLISH_GRADE,FRANCE_GRAE
from (some complex query that have order by and returns 100 rows) WHOLE_ROWS
where
//Here I need to loop over WHOLE_ROWS and make something like that:
//if(currentRow.ENGLISH_GRADE==WHOLE_ROWS(0).ENGLISH_GRADE)
//fetch this row
Basically, you need to join your query to itself. You can do with with a subquery factoring clause:
WITH complex_query AS
( ... complex query here ... )
SELECT
FROM complex_query cq1
WHERE cq1.english_grade = ( SELECT english_grade FROM cq1
WHERE rownum = 1 )
Here is a SQL Fiddle. You could also do this with analytics, but those seem to me more difficult to understand.

Resources