-2

I have a table with following fields:

ID,tomato1,tomato2,tomato3

tomato1,2,3 are text columns. Each tomato1 entry is unique, ID is autoincrement int, everything else is VARCHAR I am given tomato1 only.

I am using the query:

SELECT * WHERE tomato1='whatever'

I hear some people on the internet who say don't use SELECT * so I want to ask, Is this a bad way to do this? IS there a more efficient query code/method?

Konerak
  • 38,301
  • 12
  • 96
  • 116
David19801
  • 10,694
  • 24
  • 81
  • 123

4 Answers4

1

The asterisk means that you're always selecting all the columns from this table. So if you only want the entry of tomato3, your query should read : SELECT tomato3 FROM tomatoes WHERE tomato1='whatever'

weltraumpirat
  • 22,414
  • 5
  • 39
  • 54
  • I see I think. My goal is: Given tomato1 to return tomato2 and tomato3. so is : SELECT tomato2 and tomato3 FROM table1 WHERE tomato1='whatever' correct? – David19801 Dec 22 '10 at 12:08
  • `SELECT tomato2, tomato3 FROM table1 WHERE tomato1 = 'whatever'` (no `AND` in your select clause, just comma separated) – Konerak Dec 22 '10 at 12:09
0

Generally just query for data you need, because it limits amount of transmitted data, so SELECT ID, tomato2, tomato3 WHERE tomato1 = 'whatever' might be better.

Michal Čihař
  • 9,466
  • 3
  • 47
  • 85
0

It's mainly down to retrieving more data than is required, and thus a performance hit. Do you use the data from all of the columns? It could help with code maintance too, as you are explicitly calling everything rather than a generic '*'.

piddl0r
  • 2,461
  • 1
  • 23
  • 35
0

Additionally, if you change your table in the future, and your application is expecting only the fields that exist today, SELECT * will bring fields the application doesn't expect. SELECT with named fields is more mantainable.

erickrf
  • 1,997
  • 2
  • 20
  • 42