What's wrong with this mysql query :
select *
from tpa as t1
where ( select count(*)
from tpa as t2
where t1.id = t2.id )
error :
Error Code: 1054. Unknown column 't1.id' in 'field list'
I think, as Cfreak pointed out in comment, that the alias is not visible in the subquery.
I think also that you forgot to specify some condition for your count(*) result to be equal to some-number (or other condition):
select * from tpa as t1
where
(
select count(*) from
(
select * from tpa
)
as t2
where t1.id = t2.c_id
) = 1
Change "= 1" with any numery condition you like, or this would be a silly way to rewrite this much simpler query:
select distinct * from tpa
:-)
select id, count(*) from tpa as t1
group by id
/*If you need a certain count.. add-*/
having count(*) > 2
You should not need to use a subquery in either case.
Related
Column not found: 1054 Unknown column 'expensetot' in 'field list'
Based on the query below, what is the best way to produce grandtotal without an error?
SELECT
table1.cost,
(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id) as expensetot
(table1.cost+expensetot) as grandtotal,
table3.label
FROM
table1
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
You can't use alias in select clause you must repeat the code
and you missed a comma before table1.cost + ...
and in subselect the outer table are not in scope so you should use a proper join with subquery for sum
SELECT
table1.cost,
t.expensetot,
table1.cost + t.expensetot as grandtotal,
table3.label
FROM table1
INNER JOIN (
select table2.key, sum(table2.expense) expensetot
from table2
group by table2.key
) t on t..key=table1.id
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
Maybe that would help
SELECT
table1.cost,
(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id) as expensetot,
(table1.cost+(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id)) as grandtotal,
table3.label
FROM
table1
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
or you can read about user variables in queries:
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html
Here is my query:
SELECT t1.mycol
FROM mytable t1
JOIN mytable t2
ON t1.id = t2.postid
ORDER BY value
LIMIT 10;
It works well.
Now I need to count the number of matched rows without the limitation. So I have've added this SQL_CALC_FOUND_ROWS * to the select statement and my query throws:
value is ambiguous in the order by clause.
Why? And how can I fix it?
Noted that, when I use SQL_CALC_FOUND_ROWS 1 instead of SQL_CALC_FOUND_ROWS * in the select statement, then apperantly all fine. So, is it ok to use SQL_CALC_FOUND_ROWS 1? (I'm asking this because that's SQL_CALC_FOUND_ROWS * in the documentation).
I'm not sure why you are using a self-join, but you say the query does what you want.
Your problem is simply the lack of a qualified column name in the order by. It has nothing to do with SQL_CALC_FOUND_ROWS:
SELECT SQL_CALC_FOUND_ROWS t1.mycol
FROM mytable t1 JOIN
mytable t2
ON t1.id = t2.postid
ORDER BY t1.value
LIMIT 10;
I do not know why your original query would have worked. It has the same ambiguous column name in the ORDER BY.
Give it a try:
SELECT SQL_CALC_FOUND_ROWS * from mytable t1
JOIN mytable t2
ON t1.id = t2.postid
ORDER BY t1.value
LIMIT 10;
SELECT FOUND_ROWS();
The reason why you got the error was that, you are joining table with itself, so obviously, every column would occur twice. That's why the name is ambiguous. I added quelifier t1.
Try this
SELECT SQL_CALC_FOUND_ROWS t1.* from mytable t1
JOIN mytable t2
ON t1.id = t2.postid
ORDER BY t1.value
LIMIT 10;
SELECT FOUND_ROWS();
Is there a way to tell MySQL that while making something like this
SELECT id, MAX(seq) FROM t1 GROUP BY ident;
I can also get the id value? I know I shouldn't be using id if it's not in a group by but I feel like its strange to make a multi pass to get the row ids with the maximum seq field when it already passed it. So what is the most effective way to do this? id is the primary key
SELECT a.*
FROM tableName
INNER JOIN
(
SELECT ident, MAX(seq) seq
FROM tableName
GROUP BY ident
) b ON a.ident = b.ident AND
a.seq = b.seq
Mabye:
SELECT MAX(a.seq), (SELECT id FROM t1 as b where b.ident=a.ident AND MAX(a.seq) = b.seq LIMIT 1) as id FROM t1 AS a GROUP BY a.ident;
Fiddle
Try using self-join:
SELECT t1.* FROM MyTable t1
JOIN
(SELECT ident, MAX(seq) AS MAX_Seq
FROM MyTable
GROUP BY ident
) t2
ON t1.seq = t2.MAX_Seq
AND t1.ident = t2.ident
See this sample SQLFiddle
What is seq exactly ?
I guess you can also order your results ?
SELECT id FROM t1 GROUP BY ident ORDER BY seq DESC
Regarding to the others answer, seq is in another table ?
I wanna run a subquery that uses the value of the outer query in its where clause. Here's and example of what I wanna do:
SELECT * FROM `tbl1`
WHERE `tbl1`.`max_count` < (
SELECT COUNT(*) rc FROM `tbl2`
WHERE `tbl2`.`id` = `tbl1`.`id
)
There is tbl1 with a column named max_count, and there is tbl2 with rows referring to a row in tbl1(many-to-one relationship). What I wanna do is select rows in tbl1 where the number of rows in tbl2 referencing it is less than the max_count value of that row. But I'm pretty sure that what I wrote here, ain't gonna cut it. Any ideas?
Thanks a lot
try this -
SELECT * FROM `tbl1` t1
WHERE t1.`max_count` < (
SELECT COUNT(*) FROM `tbl2` t2
WHERE t2.`id` = t1.`id`
)
try using JOIN.
SELECT DISTINCT a.*
FROM tb1 a
INNER JOIN
(
SELECT id, COUNT(*) totalCount
FROM tbl2
GROUP BY id
) b ON a.ID = b.ID
WHERE a.max_count < b.totalCount
As an alternate solution, it's probably easier to just use a LEFT JOIN with HAVING than a subquery;
SELECT tbl1.*, COUNT(tbl2.id) current_count
FROM tbl1
LEFT JOIN tbl2
ON tbl1.id=tbl2.id
GROUP BY tbl1.id
HAVING COUNT(tbl2.id) < max_count
An SQLfiddle to test with.
Note that the GROUP BY in this case is a MySQL only thing, normally you'd need to GROUP BY every selected field in tbl1 even if tbl1.id is known to be unique per row.
select * from tablename
where id in(select id from tablename2 where condition UNION select -1)
Is it ok to use select -1 as if the inner query does not result anything it will give error. It is feasible or not?
imho, inner-select is far from ideal (slow)
based on your posted SQL, an inner join will do the trick
select *
from tablename as t1
inner join tablename2 as t2
on t1.id=t2.id
where condition; --- your condition
If you have to get it done with a subquery then the correct way to do it would probably be:
SELECT *
FROM tablename AS t1
WHERE EXISTS
(SELECT id
FROM tablename2 AS t2
WHERE conditions)
It won't give an error if the query returns nothing. It just returns an empty resultset.