+ 1

Mysql SELECT statement with condition. Which is better for showing data?

case 1 a. for showing all data SELECT id, name FROM table b. for showing data by keyword SELECT id, name FROM table WHERE id LIKE '%keyword%' case 2 for showing and search (empty keyword for showing all data) SELECT id, name FROM table WHERE id LIKE '%keyword%' all this statement showing the same result. i wonder is 'case 2' bad? more info: in case 1, i need to split query. one for show all data and the other for filter some data. in case 2, i just need 1 query for show and search. more simple.

8th Dec 2017, 7:58 AM
Andika Romansyah
4 Answers
+ 3
SELECT statement with condition, which is better? can you please elaborate some details as to better in what way? Let's review the queries, first, query 1.a is not a SELECT statement with condition, as there is no WHERE clause, furthermore, to select all data you can use SELECT * FROM table instead. Then query 1.b and query 2, these queries appear similar to me, not so sure why you think query 2 is "bad". And the results from executing query 1.a will be different with the execution results of query 1.b and query 2, because query 1.b and query 2 has a condition to filter the results, defined by WHERE clause and LIKE operator. Unless all the records in the table has similar id field value, query 1.a will return all the records, while query 1.b and 2 will return only filtered records, that match the condition, or no record at all, if there's no record found to match the condition. Hth, cmiiw
8th Dec 2017, 8:51 AM
Ipang
+ 3
@Andika no problem : )
8th Dec 2017, 9:48 AM
Ipang
+ 1
@Ipang i'm sorry if my question isn't much clear to you. i will add more detail.
8th Dec 2017, 9:06 AM
Andika Romansyah