I would convert the string into an array, then you can easily extract the first three moves from that:
select (regexp_split_to_array(moves, ',s*'))[:3] as first_moves, count(*)
from chess
group by first_moves;
The [:3]
part selects the first three elements of the array - you can adjust that to the number of moves you want to look at.
I am not sure what g1.moves LIKE '%s%%'
is supposed to do. If you only want to select games starting with specific moves, you can use:
where (regexp_split_to_array(moves, ',s*'))[:3] = array['e4','e5','Nf3']
You can ditch the whole regexp_split_to_array()
overhead if you declare the moves
column as an array text[]
.
If you want to pass the comparison values as a parameter through JDBC you can pass a string "{e4,e5,Nf3}"
and use
where (regexp_split_to_array(moves, ',s*'))[:3] = cast(? as text[])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…