cakePHP subquery in FROM - mysql

I need help to understand if something like this is posible on cakePHP...
I have a MYSQL query that retrive the information on the way it's needed, and it looks something like this:
SELECT *
FROM
(SELECT id ...)
JOIN
(SELECT id ...)
GROUP BY ...
So what I need to know if there is anyway on cakePHP to resolve the first FROM subquery.
I could break the query in two but I want to know if it can be solved in cakePHP using one Model->find command.
Thanks.

It cannot because your first "table" (in the FROM clause) is based on the model of the find. That being said
You can take your FROM subquery and make a SQL view out of it. Then create a Cakephp based on that view.
If your first subquery does not have a GROUP BY, the entire query can probably can be re-written without the subquery.

Related

Mysql use of alias table in where clause subquery

I've made some research but didn't find any answers to my question
Here it goes :
First of all I've managed to make my query work but I don't find it beautiful so that's why I'm comming here to see if I can code it better.
to summerise the query, this is what I want to do
SELECT *
FROM (/* big query with subqueries and UNION of big query with subqueries */) AS fake_table
WHERE position IN (SELECT MAX(position) FROM fake_table)
but it doesn't want to have a table alias in the subselect of the clause it say : Table 'blabla.fake' doesn't exist. I have to copy the complete big query in the clause subselect
do you guys have an idea ?
Many thx for your help

Drupal views sorting with two criteria

I want to alter my view query to change my query view.
So i want to add a query like:
<select From (select from where) order by) order by >
to just order my data twice.
Also is there any other way to do what I want even if not using the hook_view_query_alter?
I tried a lot of methods and modules (views_php, views_raw_sql, view_field_View) and
also tried stored procedures but did not know how to call it in the Drupal view
Any idea or hint please?
You don't need a nested SELECT clause to add another sort criteria to the query. The following syntax will do what you want:
SELECT ...
FROM ...
ORDER BY date1, date2
This will sort the results by date1 and then by date2.
In Views, simply add another sort criteria to the View you're editing, it's that simple.

Why does this SQL Query not work for me?

I have some problems with an (My)SQL query. In my DB I have two tables. One is called kfz_typen, the other kfz_temp2. I need to get all entries of the kfz_temp2 table, which ktyp (just an integer field) is not in the kfz_typen table.
SELECT * FROM kfz_temp2
WHERE kfz_temp2.KType NOT IN (SELECT DISTINCT kfz_typen.ktyp FROM kfz_typen)
In my opinion this query above should exactly do, what I want. But it doesn't! I just get an empty result back from my MySQL Server.
Without the "NOT" in the Query, I get the entries that are in both tables, so the matching does work.
So what's wrong with the Query above?
Would this work?
SELECT t2.* FROM kfz_temp2 t2
LEFT JOIN kfz_typen tn ON t2.KType = tn.ktyp
WHERE tn.ktyp IS NULL
You may need to group the result.
I'm unsure why the first query doesn't work, but I believe this does the same thing.
remove distinct then i hope it is helpful to you.

How do I use a field that is a result of a subquery within a condition?

Here is the query I'm trying to execute, and it's supposed to return a table containing data for the pools that are not full (members_nr < members_max).
SELECT id, name,
(
SELECT COUNT(*) FROM pools_entries WHERE pool_id=p.id AND pending=0
) AS members_nr,
members_max, open
FROM pools p
WHERE id IN(1,2,3,4) AND members_nr < members_max;
The problem is MySQL won't recognize members_nr as a field since it's a result from a subquery. Is there a logic solution to this little issue?
Any help will be much appreciated :)
N.B. is correct, you need the having clause. But for the sake of the googler's i'll share a little knowledge.
The WHERE clause is used for restricting the resultset to specific records, it is also used for optimisation. Mysql uses the WHERE clause to identify which index's it can use to speed up the query.
The HAVING clause is executed right at the end of the query. It is used for filtering the recordset. So imagine you have a list of stuff from the database that matches your WHERE clause. You can then use HAVING to filter that list down further on some set conditions.
My basic rule of thumb is: if you need to select based on a column's value, use WHERE, if you need to select based on the value of something which is not a column in the table, use HAVING.

MYSQL WHERE-IN Subquery Runs Forever

I have a MySQL table. Let's call it Widgets. The Widget table has 3 fields: id, type_id, and name. I want, in one query, to get all the widgets that share a type_id with the Widget named 'doodad'. I've written 2 queries:
Give me the type_id of the widget with the name 'doodad'.
Give me all widgets with that type_id.
This works. Each query, independently achieves its goal.
But when I combine them into a single nested query, it runs forever, infinite loop style. It looks like this:
SELECT * FROM widgets WHERE type_id IN (
SELECT type_id FROM widgets WHERE name = 'doodad'
);
Can anyone explain this? Is it because I am writing a nested query which is operating on the same table twice?
Little wheel, why spinnest thou?
There is an issue in MySQL and in where even uncorrelated subqueries are treated as though they were correlated and re-evaluated for each row.
In the explain plan the select type will likely be showing as dependant subquery rather than just subquery as would be desired.
I suggest trying the approach described at the end of this article of using a derived table to materialize the inner result set.
Or alternatively you could look at the constify procedure here to see if it will assist you in getting around this issue.
Using a JOIN risks duplicating results - an EXISTS will work similar to an IN, without the duplication risk:
SELECT x.*
FROM widgets x
WHERE EXISTS (SELECT NULL
FROM WIDGETS y
WHERE y.name = 'doodah'
AND y.type_id = x.type_id)