Mysql use of alias table in where clause subquery - mysql

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

Related

cakePHP subquery in FROM

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.

mysql select distinct *

I'd rather post an image here, but it says that I don't have enough reputation to do it.
I tried to find something similar to my question, but there are to much "distinct" and "group by" to find something useful... "distinct *" in search gives the same, as "distinct"..
Here is link to xlsx-file with table with source example data and table with desired result
Source example data - is a simplified result of some complex query.
The question is here:
I'd like to apply to this result of select some grouping, which gives query "select distinct * from table_below".
But, this variant is not very good for performance reason. My original non-simplified table has about 4000 rows, and 10 columns. So, "select distinct *" takes 20 sec to give needed result.
To be clear, I want to make grouping by 4-th column, but within every "column_id", as shown in xlsx attached file.
Thanks in advance
It is better to write out the coloumn names. Could you try this?
SELECT DISTINCT coming_id, spare_id ,spare_sum, product_id
FROM Table
Order by coming_id, spare_id, product_id

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.

sub query or join?

I am wondering if anyone can explain to me when you would use a sub query and when you would use a join.
for example.
I have this query:
SELECT * from contacts where id in (select contactId from contactgrouplink where groupId = 1);
What would the benefit be of a join over this sub query?
Look at here well discussed
Subqueries vs joins
select * from contacts x, contactgrouplink y where x.id=y.contactId and y.groupId=1
Use EXPLAIN just before each of those query's... to see what query does!
Do an EXPLAIN, my rule of thumb is to try to get rid of DEPENDENT SUBQUERY since that means for each row in the outer SQL statement, a query is executed. Also, you can try to implement it as a join and see how many rows each version would examine and make the call from there.
In my knowledge, Sub-query is batter than join .
I'm also use Sub-query Becoz "Join" is Effecting performance. (According My-Sql Preference)

Sql count without duplication in statment

i have an sql query that selects a bunch of data. I would also like to get the number of records selected by the query (before i limit it). All the examples i have seen of the count statment duplicated the select. My select statment is about 50 lines long and i would rarther not duplicate it.
Thanks
Your question would be easier to answer if you could give us an example SQL statement, however, from what you have said so far, the following should be correct:
Select Columns, Count(Distinct Value) From Table Where x=y Group By Columns
Yes.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
It isn't really possible to get the number of rows that a query would return without running it or a version of it.
There's sql_calc_found_rows which will let you put a limit clause on the statement and return the total number of rows it would have found had there not been a limit clause in a subsequent call to found_rows(), but it's expensive.
Thanks everyone, i was just trying out sql_calc_found_rows, and your dam right Nick, it is expensive. I think ill just create a separte query, thanks