tags:

views:

93

answers:

8

What does the below query explain?

SELECT * FROM `jos_menu` WHERE (id = 69 OR id = 72)

I know its very silly question, but sometimes easy things creates mess in my skulls interpreter.. Pls help

EDIT

Its giving me record for both IDs, why is it doing so? It should five me the record for either 69 or 72....

+1  A: 

It will ask TABLE jos_menu for all the records that have an ID of 69 or 72.

Shadow
Sorry its not doing this... budddy u r wrong... its giving me records for both
OM The Eternity
Yeah, it will select where the record is either 69 or 72, thus records for both.
Shadow
That's correct. It will give you both records because it's not an exclusive 'or'. i.e. it doesn't stop processing the moment it finds a record. You will have to look into a different technique if you just want one record with either of those ids.
Derek Clarkson
Thanks @Derek and @Shadow
OM The Eternity
There's a number of techniques you can employ, mostly sql dialect specific. One is to use the limit parameter to specify just to return one record. Another is to use the first aggregate function. You need to look at what options your sql engine is offering. Alternatively you can bring both records back to the code and just use one of them.
Derek Clarkson
A: 

Get all the columns from the table called jos_menu where the id column is equal to 69 or 72.

Billy ONeal
Don't you mean get all rows ? ;)
Shadow
@Shadow: No, actually I didn't. I was translating SELECT *. Why people continually use * is beyond me. That's an anti pattern that needs to go...
Billy ONeal
Sorry while it selects all columns '*' it returns all rows.
Shadow
A: 

It lists all the rows from the table jos_menu where the column named id has value 69 or 72

So effectively you'll get rows for both id's

codaddict
+6  A: 

I think you mean "Explain what the below query does", which I'll do:

  • Return data from all of the columns in the jos_menu table. Return all rows whose id's are 69. Also return all rows whose id's are 72.

Essentially what it does is it goes through each row and checks:

  1. Does this row have an id of either 69 or 72? If yes to either one, add it to the list of items returned.
  2. Does this row have an id of either 69 or 72? If yes to either one, add it to the list of items returned.
  3. Does this row have an id of either 69 or 72? If yes to either one, add it to the list of items returned.

etc..

Cam
Absolutely the same explanation i was looking for, Thanks @incrediman, U r incredible in reading my mind.... Thanx Mate
OM The Eternity
+1 for a clear and simple explanation
ggfan
+6  A: 
SELECT *

Get the values of every column

FROM `jos_menu`

From a table called "jos_menu"

WHERE (id = 69 OR id = 72)

And only fetch records that have an ID of 69 or 72

Edit: In response to your edit ("Its giving me record for both IDs, why is doing so?"), you might want to try using the LIMIT keyword in your Sql. This will only grab the first match and then stop. For example:

SELECT * FROM `jos_menu` WHERE (id = 69 OR id = 72) Limit 1
Greg W
orright!!!! Point Noted.. Greg...
OM The Eternity
(+1) Although note that if you have both a 69 and a 72, the behaviour of that query is fairly undefined unless you add an `ORDER BY` - for example: `SELECT * FROM \`jos_menu\` ORDER BY id ASC WHERE (id = 69 OR id = 72) Limit 1`. Of course the behaviour is still pretty undefined if there can be multiple rows with the same id's, in which case you might want to `ORDER BY` something else as well.
Cam
Yep, thats a good point. I generally assume my ID's are in numeric order in the table (since they're auto_increment'd anyway) but its best to add the ORDER BY just in case.
Greg W
@Greg I was actually not going by the assumption that the id was necessarily the primary key. Now that I think about it though, it likely is. And in that case my last sentence about duplicate id's also is not applicable :)
Cam
But still it's a good point. Though this might not be an issue in this case you should always take into account what kind of indeterministic behaviour you "allow".
VolkerK
A: 

Select all records from jos_menu where ID is either 69 or 72.

simoncpu
A: 

The Query will return the records from the jos_menu table which satisfy the condtion that id column that is exisiting in jos_menu table having the id's as either 69 or 72.

A bettter way of writing it is

SELECT * FROM jos_menu WHERE id in ( 69 ,72) 

here in the above query u can add any number of id's u want..

to limit to only one record

SELECT Top 1 * FROM jos_menu WHERE id in ( 69 ,72) 
Phani Kumar PV
A: 

Just read it out loud remembering what the wildcards mean.

I'd read this as:

"Select all records from the table 'jos_menu' where the 'id' attribute is equal to 69 or 72"

Andrew