How to write a query correctly to get data without uniqueness?
I have list of ids, where ids are repeated.
Example: (1,1,1,2,3)
select *
from table
where id in (1,1,1,2,3);
returns only (1,2,3).
But I need to get with repeated entries.
Use a derived table and left join:
select t.*
from (select 1 as id union all select 1 union all select 1 union all select 2 union all select 3
) i left join
t
on t.id = i.id
The syntax for the derived table might vary depending on the database, but most support the above syntax.
That's not what WHERE statement is for, as it's only for filtering matching keys.
If you need to do that in this order, use sth like
select table.*
from (
select 1 as id
union select 1
union select 1
union select 2
union select 3
) myStaticKeys
join table using (id)
Related
When I use this I Get the table two times but I want each value duplicated before the next value comes
SELECT *
FROM student
WHERE major LIKE 'C%'
UNION ALL
SELECT *
FROM student
WHERE major LIKE 'C%';
You can use a union all. A union will not work, because it will eliminate duplicates. Another way is a cross join:
select
*
from
students t1 cross join
(select 1 as n union all select 2) n
WHERE major LIKE 'C%'
order by id;
The following query:
SELECT * FROM table WHERE id IN (1,2,3);
will return three records.
SELECT * FROM table WHERE id IN (1,2,1);
will return just two records (for Ids 1 and 2)
Is there a way for the result set to contain two records for Id 1 (and three in total)?
You could try creating a table for the ids you want to filter by. This would get you your desired results. I'm not sure if mysql supports CTE, but hopefully this is enough for you to get the idea.
WITH IDS
AS
(
SELECT 1 AS id
UNION ALL
SELECT 2 AS id
UNION ALL
SELECT 1 AS id
)
SELECT T.*
FROM T
JOIN IDS
ON T.id = IDS.id
I want the count even if the count is 0. My current query is
SELECT `id`,count(0) as `fetchpc` FROM `user` WHERE pid in('4,6,7,8') GROUP BY `id`
But it returns only those id where count is greater than 0
Edit:
the values used for in('4,6,7,8') are first fetched from database in another query. And then using a script rows are converted to 4,6,7,8.
So all the values are present in the database.
Also it is possible that the values returned can go upto 100+ values.
You could left join this query on a "fictive" query that queries these IDs as literals:
SELECT ids.id, COALESCE(cnt, 0)
FROM (SELECT 4 AS id
UNION ALL
SELECT 6 AS id
UNION ALL
SELECT 7 AS id
UNION ALL
SELECT 8 AS id) ids
LEFT JOIN (SELECT id, COUNT(*) AS cnt
FROM fetchpc
GROUP BY id) t ON t.id = ids.id
You can use a derived table. I would recommend:
SELECT i.id, COUNT(u.id) as fetchpc
FROM (SELECT 4 as id UNION ALL
SELECT 6 as id UNION ALL
SELECT 7 as id UNION ALL
SELECT 8 as id
) i LEFT JOIN
`user` u
ON u.id = i.id
GROUP BY i.id;
From a performance perspective, this is much better than aggregating first (in a subquery) and then joining. Basically, the aggregation (in that case) has to aggregate all the data and afterwards filter out the unnecessary rows.
This formulation filters the rows first, which should speed the aggregation.
I have Two tables: left one is users_projects, right one is projects:
I want to select the projects that user 3 is not participating in (only p_ID 5 and 7).
I've tried SELECT * FROM users_projects up INNER JOIN projects p ON p.p_ID=up.p_ID WHERE up.u_ID!=3
but that also returns me p_ID 1 which both user 2 and 3 are a part of.
Thanks for your help!
A solution with LEFT JOIN:
SELECT
*
FROM
projects p LEFT JOIN users_projects up ON (p.p_ID = up.p_ID AND up.u_ID = 3)
WHERE
up.u_ID IS NULL
Basically select all Projects and join them with the user_projects of the desired user. Left join makes all rows from the project table appear even if the is no corresponding row in the users_projects table. These rows have all fields from the users_projects set to NULL, so we can just select those.
This is not a JOIN query, but a query with a non-correlated sub-select with a NOT IN() predicate.
I hope the columns of the projects table are enough ...
SELECT
*
FROM
( SELECT 1,'Apple' -- input data, don't use in 'real' query
UNION ALL SELECT 5,'Banna' -- input data, don't use in 'real' query
UNION ALL SELECT 7,'Carrot' -- input data, don't use in 'real' query
UNION ALL SELECT 8,'Durian') -- input data, don't use in 'real' query
projects(p_id,p_name)
WHERE p_id NOT IN (
SELECT
p_id
FROM
( SELECT 2,1 -- input data, don't use in 'real' query
UNION ALL SELECT 2,5 -- input data, don't use in 'real' query
UNION ALL SELECT 2,7 -- input data, don't use in 'real' query
UNION ALL SELECT 3,1 -- input data, don't use in 'real' query
UNION ALL SELECT 3,8) -- input data, don't use in 'real' query
users_projects(u_id,p_id)
WHERE u_id=3
)
;
p_id|p_name
7|Carrot
5|Banna
I am trying to exclude rows in my table based on the id's in other tables.
I have 2 tables of which a "select * from" results in a set like (1,2,3)
I am trying to combine the results from these 2 subqueries into one, like:
(1,2,3) + (4,5) = (1,2,3,4,5)
So I can filter the big table with a "NOT IN (1,2,3,4,5)"
I have been looking at GROUP_CONCAT's, UNION and all other kinds, but I can't seem to find something that actually works.
Anyone have a idea?
select *
from Table3
where id not in (
select id from Table1 --your subquery that returns 1,2,3
union all
select id from Table2 --your subquery that returns 4,5
)
select * from mytable
where id not in (
select id from othertable
union
select id from othertable2
)