Forum Moderators: coopster
id name state
1 Jake available
2 Jake unavailable
3 Ann available
4 Ann unavailable
5 Zak available
Suppose I'm user = Jake. I want to select all records that are mine or available:
SELECT name FROM tbl WHERE name='Jake' AND state='available';
However I would like to place them not in random order, but user's records first, and then if possible other sorted by creation.
To select by creation is easy - just ORDER BY id DESC, but how to order so, that Jake is first?
The output should be:
1,2,5,3 (- id)
Best regards
Michal Cibor
PS. A question concerning this forum: How do you quote stuff? I mean parts of previous messages with grey background and a border?
Thanks!
Maybe what you need is to do:
SELECT name FROM tbl WHERE name='Jake' OR state='available' ORDER BY name, state;
Your first suggestion is valid, but I did a (rare) test before posting, using simply UNION on one of my existing tables, and it did produce the results that mcibor wants. When using UNION on two different tables, however, it might not, but on on one table it does.
Also, your suggestion,
SELECT name FROM tbl WHERE name='Jake' OR state='available' ORDER BY name, state;
...will not put 'Jake' at the top as mcibor wishes.
I wish you well,
Salsa
To the powers that be: I tried to edit my last post, but the "Owner Edit" link was not available. (This complaint is probably for naught, however, as I'll bet that all of the administrators are at PubCon:)!
Edit: This is just to affirm that my "Owner Edit" is back again. I just wish I hadn't brought up PubCon to myself, becauseI sure wish I were there! I'm still kicking myself for missing the Orlando one because it was practically out my back door.
A UNION [dev.mysql.com] is a great solution to this issue, you've got a push in the right direction, now you'll have to tweak the statement to meet your needs. Keep in mind that UNION became available in MySQL >= 4.0. Prior to that you can resolve the issue using a temporary table [dev.mysql.com].
The Owner Edit link is on a timed delay, disappearing after a period of time that has been set by the WebmasterWorld Forums owner.
Sorry for making a mistake in WHERE statement. Certainly it should be OR not AND. Hope it didn't cause any problems.
Best wishes all!
Micha³ Cibor
Best regards
Micha³ Cibor
Coopster: Thanks for the Owner Edit explanation. I see now that no older posts have the Owner Edit option. I guess I'd never tried to edit a post more than minutes old before and hadn't noticed that the option disappeard after a bit. The time delay is a good scheme to preserve continuity.
[b]all the stuff[b] in [i]help[i]
You're missing the "slash" in the closing BBtag.
[ b ]bold[ /b ] (I used extra spaces so you can see)
[ quote ]Quoted stuff ...[ /quote ]
Just like HTML except using square brackets instead of angle brackets. If you have proper opening and closing tags, you'll see the result in "Preview" before submitting. :)