mysql - How do I get all rows meeting specific criteria? -


i'm trying, in single query, entire set of rows when 1 of rows meets criteria. in case below, want query mike smith. if find mike smith has taken test (by test_id) want include results test_id. successful query return first 7 rows. possible without running multiple queries? below example entire contents of table.

i can't use

select * first_name = 'mike';  

as return mike's test scores;

i don't know how select test scores (for multiple tests) when have result mike.

+------------+------------+-----------+-------+------+ | test_id    | first_name | last_name | class | rank | +------------+------------+-----------+-------+------+ | 1          | john       | doe       | 2012  | 1    | +------------+------------+-----------+-------+------+ | 1          | jack       | smith     | 2014  | 50   | +------------+------------+-----------+-------+------+ | 1          | mike       | smith     | 2014  | 60   | +------------+------------+-----------+-------+------+ | 2          | mike       | smith     | 2014  | 70   | +------------+------------+-----------+-------+------+ | 2          | john       | smith     | 2014  | 80   | +------------+------------+-----------+-------+------+ | 3          | jake       | smith     | 2014  | 80   | +------------+------------+-----------+-------+------+ | 3          | mike       | smith     | 2014  | 90   | +------------+------------+-----------+-------+------+ | 4          | jake       | smith     | 2014  | 78   | +------------+------------+-----------+-------+------+ 

use exists clause, eg

select * `test_table` exists (     select 1 `test_table` b     first_name = 'mike'     , last_name = 'smith'     , b.test_id = a.test_id ) 

alternatively, can inner join table itself, eg

select a.* `test_table` inner join `test_table` b     on a.test_id = b.test_id b.first_name = 'mike' , b.last_name = 'smith' 

demo here - http://sqlfiddle.com/#!2/c8646/1


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -