If I have a table like this:
id car
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 F
3 A
3 C
3 E
3 F
3 G
what I want is different "id" which have ("A" or "C") and "B" in car. For example:
id car
1 A
1 B
1 C
2 A
2 B
2 C
what I did was
select * from table where (car like "A" or car like"C") and (car like "B")
but it gives me an empty row.
Any clue?
You can use a self-join
SELECT t1.id
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.id
WHERE t1.car IN ('A', 'C')
AND t2.car = 'B'
BTW, you should generally only use LIKE when you're doing a pattern match. For exact matches use =, or IN for matching any of multiple items.
Untested, but something like this should work: get all rows that have the ID such that it is found on the list of all IDs that have an A or a C, and also on the list of all IDs that have a B.
SELECT t.*
FROM mytable t
WHERE t.id IN (
SELECT DISTINCT id
FROM mytable t2
WHERE t2.car='A' OR t2.car='C'
)
AND t.id IN (
SELECT DISTINCT id
FROM mytable t3
WHERE t3.car='B'
)
Related
table1
Agent id
A 1
B 2
C 3
D 4
E 5
table2
Agent id
C 3
D 4
output:
Agent id
A 1
B 2
E 3
Do you want not exists?
select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.agent = t1.agent and t2.id = t1.id)
This brings records of table1 for which there is no corresponding record in table2. I assumed that you want to match on both columns: you might to review that, and adapt the where clause of the subquery accordingly.
I am using a simple query:
select A,B from Table1 where id in ('');
which gives me output like:
A B
1 X
2 V
3 R
Now i want to know count of value B in whole database:
i.e
A B CountB
1 X 3
2 V 1
3 R 2
This ought to to it:
SELECT *
FROM (SELECT A,
B
FROM Table1
WHERE id in ('')) t1 LEFT JOIN
(SELECT B,
COUNT(B)
FROM Table1
GROUP BY B) t2 ON t1.B = t2.B
May I didn't understand the question but it seems the following query should do:
SELECT A,
B,
COUNT(*)
FROM Table1
WHERE .....
GROUP BY A,
B
ORDER BY A,
B;
You need to make use of group_by if you want certain column, and then add a count the id of it to get count. So I'd do
SELECT A,B,COUNT(B.ID) as CountB FROM Table1 WHERE id IN ('') GROUP BY B;
I need to make a query that selects everything from a table A, and in addition to have a column that indicates the number of times that the value of A.col1 is in B.col2.
Example:
Table A:
id name
1 "y"
2 "z"
3 "w"
Table B:
id name
15 "y"
23 "w"
14 "y"
I want a query that will give me the following:
id name numOfTimes
1 "y" 2 // y is shown twice in table B
2 "z" 0 // z isn't shown in table B
3 "w" 1 // w is shown once in table B
try this:
Select a.id, a.name, count(b.id) as numoftimes from a
left outer join b on a.name = b.name
group by a.id, a.name;
It can be done in multiple ways
select a.id as Id
,a.name as Name
,(select count(1) from b where a.id=b.id) as NumberOfTimes
from a;
or
select a.id as Id
,a.name as Name
,count(b.id) as NumberOfTimes
from a
left join b on a.id=b.id;
Try with this following you will get your expected result.
select a.id,a.col1,count(b.col1) as numOfTimes
from TableA a left join TableB b
on a.col1 = b.col1
group by a.id,a.col1;
Thanks.
Suppose you have two tables:
table1 table2
id | cityA | cityB id | cities_queue|
1 a c 1 a , b , d
2 s f 2 a , b , c ,e
3 d m 3 a , m , d , e
I want to return only those rows of table 2 that includes cityA and cityB with this specific order, first cityA and after ( somewhere...) comes cityB.
Because of that the only accepted result must be (a , b , c, e). First thought is to use LIKE command to find which table2 rows include cityA, then using substr() to receive only part of cities_queue that comes after cityA and using LIKE() again for cityB. So my question is: is it possible to use only once LIKE() holding as a string something like
(cityA) - % - (cityB)
where % = wildcard
to find all cities_queue that include cityA and cityB regardless of what is between them; If so, please provide the right syntax. Thanks
Not sure what is your point is but if order of elements has really important:
http://sqlfiddle.com/#!9/83459/7
SELECT t2.*
FROM table2 t2
INNER JOIN table1 t1
ON t2.cities_queue LIKE CONCAT('%',t1.cityA,' , ',t1.cityB,'%')
or
SELECT t2.*
FROM table2 t2
INNER JOIN table1 t1
ON t2.cities_queue LIKE CONCAT('%',t1.cityA,' % ',t1.cityB,'%')
Similar to #Alex, but I'd go with a regex:
SELECT *
FROM table2 t2
INNER JOIN table1 t1
ON t2.cities_queue regexp CONCAT(t1.cityA,'.*',t1.cityB)
http://sqlfiddle.com/#!9/83459/3
Lets say I have a column named source in a table x. Individual entries can be like;
Id c1 c2 source ...
1 a b something
2 b a something
3 a b somethingelse
4 c a somethingelse
5 a b something
6 b c something
How can I delete entries with less than 3 same elements in source? For example since source value somethingelse appears 2 times, I need all entries that have somethingelse removed.
DELETE a
FROM tableName a
INNER JOIN
(
SELECT source
FROM tableName
GROUP BY SOURCE
HAVING COUNT(*) < 3
) b ON a.source = b.source
SQLFiddle Demo
One more thing to do for faster performance, add an INDEX to column SOURCE.
Roughly something like this would do the job
DELETE FROM TABLE_T1 WHERE ID IN (
SELECT ID FROM TABLE_T1 GROUP BY SOURCE HAVING COUNT(*) < 3
)
DELETE id
FROM yourtable a
JOIN (
SELECT *
FROM yourtable
GROUP BY
source
HAVING COUNT(*) > 3
) b
ON a.id = b.id
DELETE FROM x WHERE id IN
( SELECT id FROM
( SELECT id, COUNT(source) AS n
FROM x GROUP BY source
HAVING n < 3
)
)