Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple rows with the same value(s)

Tags:

sql

rows

I have a table, sort of like this:

ID  |  Chromosome | Locus | Symbol | Dominance |
===============================================
1   |      10     |   2   |   A    |   Full    |
2   |      10     |   2   |   a    |   Rec.    |
3   |      10     |   3   |   B    |   Full    |
4   |      10     |   3   |   b    |   Rec.    |

I'd like to select all rows with the same locus and chromosome. For example, rows 3 and 4. There may be more than 2 at a time and they may not be in order.

I tried this:

SELECT *
FROM Genes
GROUP BY Locus
HAVING Locus='3' AND Chromosome='10'

But it always returns row 3, never row 4, even when repeated. I think I'm missing something obvious and simple, but I'm at a loss.

Can someone help?

like image 948
user1202673 Avatar asked Feb 10 '12 18:02

user1202673


People also ask

How do I select more than one row in SQL?

SELECT * FROM users WHERE ( id IN (1,2,..,n) ); or, if you wish to limit to a list of records between id 20 and id 40, then you can easily write: SELECT * FROM users WHERE ( ( id >= 20 ) AND ( id <= 40 ) ); I hope this gives a better understanding.

Which one if this is used to put the same value in all the rows?

The SQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It does not remove duplicate rows between the various SELECT statements (all rows are returned). Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.

What is S in SQL query?

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".


2 Answers

You need to understand that when you include GROUP BY in your query you are telling SQL to combine rows. you will get one row per unique Locus value. The Having then filters those groups. Usually you specify an aggergate function in the select list like:

--show how many of each Locus there is SELECT COUNT(*),Locus FROM Genes GROUP BY Locus  --only show the groups that have more than one row in them SELECT COUNT(*),Locus FROM Genes GROUP BY Locus HAVING COUNT(*)>1  --to just display all the rows for your condition, don't use GROUP BY or HAVING SELECT * FROM Genes WHERE Locus = '3' AND Chromosome = '10' 
like image 159
KM. Avatar answered Oct 13 '22 14:10

KM.


Assuming that you want all rows for which there is another row with the exact same Chromosome and Locus:

You can achieve this by joining the table to itself, but only returning the columns from one "side" of the join.

The trick is to set the join condition to "the same locus and chromosome":

select left.*
from Genes left
inner join Genes right
on left.Locus = right.Locus and 
  left.Chromosome = right.Chromosome and left.ID != right.ID

You can also easily extend this by adding a filter in a where-clause.

like image 29
Matt Fenwick Avatar answered Oct 13 '22 14:10

Matt Fenwick