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.
Related
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();
I have a table with 17 records. I select rows by a where condition such as below query:
SELECT * FROM T1 WHERE (c1='AA' and c3=13)
2 records were the results of this query.
now i want to select the 15 records of table T1!!!
i add the not operator before the where condition like:
SELECT * FROM T1 WHERE not (c1='AA' and c3=13)
14 records were the results of this query. but the Table has 17 records
it's not possible to use EXCEPT clause, because this clause only selects distinct records.
What if i use EXISTS ?? for SQL Server
select * from t1 t
where not exists(
select * from t1 where (t.c1='AA' and t.c3=13)
)
In case Id is Duplicate then Use Row_number() function.
SELECT T1.* FROM T1
LEFT JOIN (SELECT * FROM T1 WHERE (c1='AA' AND c3=13)) AS T2
ON(T1.ID=T2.ID) WHERE T2.ID IS NULL;
Please try like this, EXCEPT Clause
SELECT DISTINCT * FROM
(
SELECT * FROM T1
EXCEPT
SELECT * FROM T1 WHERE (c1='AA' and c3=13)
)k
SELECT * FROM t1
WHERE PrimaryKey_Col not in (
SELECT PrimaryKey_Col FROM t1 WHERE t.c1='AA' and t.c3=13
);
Note : This query easily work Based on PrimaryKey
I'm trying to search for all entries in one table where they have a column that matches entries in another column that precede a -
Example Output:
This is the query I tried, it returned the error of "Error in query (1242): Subquery returns more than 1 row"
SELECT * FROM table1
WHERE
table1.Column1 = (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
You can use IN in your WHERE clause :
SELECT * FROM table1
WHERE
table1.Column1 IN (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
Another way is to use JOIN as
SELECT * FROM table1 t1
inner join (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1) as str
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
)t2
on
t1.column1 = t2.str
;
DEMO
I have a Query which returns comma separated integers
like :
select GROUP_CONCAT(ids) from table2
now I want to use that result in another query
like :
select * from table1 where column in (select GROUP_CONCAT(ids) from table2)
in this case it will consider only first value in IN clause.
I agree with #Alma that this can't be done with IN, you might be able to do it with FIND_IN_SET, but if you can do it with IN it's probably a better approach :
SELECT *
FROM table1
WHERE find_in_set(ids, (
SELECT GROUP_CONCAT(ids)
FROM table2
)) != 0;
sqlfiddle demo
Any special reason not using a join and using a sub query
select * from table1 t1
JOIN table2 t2 on (t2.column = t1.ids)
Have a table where certain rows come in couples which have a matching GUID. Just wondering how to SELECT all data from the table but ONLY if the rows exist as a couple with a matching GUID.
You can use a query like this:
SELECT *
FROM yourtable
WHERE GUID IN (SELECT GUID FROM yourtable GROUP BY GUID HAVING COUNT(*)=2)
The subquery will return all GUIDs that appears exactly twice, the outer query will return all rows associated to those GUIDs.
Please see fiddle here.
Try something like this:
SELECT t1.*
FROM
table t1
, table t2
WHERE
t1.guid = t2.guid
AND t1.id <> t2.id
;
table: your table name
id: some field that you know is different for both rows
Try
SELECT t.*
FROM Table1 t JOIN
(
SELECT guid
FROM Table1
GROUP BY guid
HAVING COUNT(*) = 2
) q ON t.guid = q.guid
Here is SQLFiddle demo