/    Sign up×
Community /Pin to ProfileBookmark

ording with SQL and php

i am pulling data from a an MySQL data base that i want to order in some useful way but as fare as i can tell if try and order the data on more than one field name the data still looks a bit scrambled. here is my SQL:

SELECT * FROM news ORDER BY cleint_text AND date_year DESC;

and this is what i get back:

year cleint_text product_text

2005 Fuller Smith and Turner product 2
2005 Noon products Ltd product 2
2005 Fuller Smith and Turney product 4
2005 Fuller Smith and Turner product 3
2005 Fuller Smith and Turner product 5
2003 sainsburys product 1
2003 Fuller Smith and Turner product 2
2004 Sainsburys product 2
2005 Sainsburys product 3
2003 Noon products Ltd product 1
2003 noon products Ltd product 2
2003 premier foods product 3
2003 Fuller Smith and Turner product 3
2003 Sainsburys product 4
2004 Sainsburys product 1
2004 Fuller Smith and Turner product 6
2004 Sainsburys product 6
2004 Fuller Smith and Turner product 5
2004 Sainsburys product 2

but this is what i would like to get back: the data separated out into years the client and then possible product text. i could do it be selecting distinct dates and then running separate SQL query on each year but that seems like a bit of along winded way of doing things. or is there some kind of magic php array function i don’t know about any ideas?

this is what i want:

year cleint_text product_text

2005 Fuller Smith and Turner product 2
2005 Fuller Smith and Turner product 3
2005 Fuller Smith and Turney product 4
2005 Fuller Smith and Turner product 5
2005 Noon products Ltd product 2
2005 Sainsburys product 3

2004 Fuller Smith and Turner product 5
2004 Fuller Smith and Turner product 6
2004 Sainsburys product 1
2004 Sainsburys product 2
2004 Sainsburys product 2
2004 Sainsburys product 6

2003 Fuller Smith and Turner product 2
2003 Fuller Smith and Turner product 3
2003 Noon products Ltd product 1
2003 noon products Ltd product 2
2003 premier foods product 3
2003 sainsburys product 1
2003 Sainsburys product 4

any help will be cool

kev

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@artoAug 10.2005 — Try: [CODE]SELECT * FROM news ORDER BY date_year DESC, cleint_text, product_text;[/CODE]Arto
Copy linkTweet thisAlerts:
@BeachSideAug 10.2005 — What happens when you swap the order by?

Like this...
<i>
</i>SELECT * FROM news ORDER BY date_year AND cleint_text DESC;
Copy linkTweet thisAlerts:
@lomokevauthorAug 10.2005 — that works like a dream:

thank you so much!

$sql = "SELECT * FROM news ORDER BY date_year DESC, cleint_text, brand_text;";

id year clent produt

29 2005 Fuller Smith and Turner product 1

19 2005 Fuller Smith and Turner product 2

24 2005 Fuller Smith and Turner product 3

26 2005 Fuller Smith and Turner product 4

45 2005 Fuller Smith and Turner product 5

43 2005 Noon products Ltd product 1

20 2005 Noon products Ltd product 2

65 2005 Sainsburys product 1

49 2005 Sainsburys product 3

66 2005 Sainsburys product 4

62 2004 Fuller Smith and Turner product 4

60 2004 Fuller Smith and Turner product 5

58 2004 Fuller Smith and Turner product 6

57 2004 Sainsburys product 1

48 2004 Sainsburys product 2

61 2004 Sainsburys product 2

64 2004 Sainsburys product 3

63 2004 Sainsburys product 5

59 2004 Sainsburys product 6

47 2003 Fuller Smith and Turner product 2

55 2003 Fuller Smith and Turner product 3

50 2003 Noon products Ltd product 1

51 2003 noon products Ltd product 2

54 2003 premier foods product 3

46 2003 sainsburys product 1

56 2003 Sainsburys product 4

this what happens with

SELECT * FROM news ORDER BY date_year AND cleint_text DESC;

19 2005 Fuller Smith and Turner product 2

20 2005 Noon products Ltd product 2

26 2005 Fuller Smith and Turner product 4

24 2005 Fuller Smith and Turner product 3

45 2005 Fuller Smith and Turner product 5

46 2003 sainsburys product 1

47 2003 Fuller Smith and Turner product 2

48 2004 Sainsburys product 2

49 2005 Sainsburys product 3

50 2003 Noon products Ltd product 1

51 2003 noon products Ltd product 2

54 2003 premier foods product 3

55 2003 Fuller Smith and Turner product 3

56 2003 Sainsburys product 4

57 2004 Sainsburys product 1

58 2004 Fuller Smith and Turner product 6

59 2004 Sainsburys product 6

60 2004 Fuller Smith and Turner product 5

61 2004 Sainsburys product 2

62 2004 Fuller Smith and Turner product 4

63 2004 Sainsburys product 5

64 2004 Sainsburys product 3

29 2005 Fuller Smith and Turner product 1

43 2005 Noon products Ltd product 1

65 2005 Sainsburys product 1

66 2005 Sainsburys product 4
Copy linkTweet thisAlerts:
@BeachSideAug 10.2005 — 
this what happens with

SELECT * FROM news ORDER BY date_year AND cleint_text DESC;

19 2005 Fuller Smith and Turner product 2

20 2005 Noon products Ltd product 2

26 2005 Fuller Smith and Turner product 4

24 2005 Fuller Smith and Turner product 3

45 2005 Fuller Smith and Turner product 5

46 2003 sainsburys product 1

47 2003 Fuller Smith and Turner product 2

48 2004 Sainsburys product 2

49 2005 Sainsburys product 3

50 2003 Noon products Ltd product 1

51 2003 noon products Ltd product 2

54 2003 premier foods product 3

55 2003 Fuller Smith and Turner product 3

56 2003 Sainsburys product 4

57 2004 Sainsburys product 1

58 2004 Fuller Smith and Turner product 6

59 2004 Sainsburys product 6

60 2004 Fuller Smith and Turner product 5

61 2004 Sainsburys product 2

62 2004 Fuller Smith and Turner product 4

63 2004 Sainsburys product 5

64 2004 Sainsburys product 3

29 2005 Fuller Smith and Turner product 1

43 2005 Noon products Ltd product 1

65 2005 Sainsburys product 1

66 2005 Sainsburys product 4[/QUOTE]


That is strange because, while the other way is better because it takes the products into account, this way should still have output something like this

19 2005 Fuller Smith and Turner product 2

26 2005 Fuller Smith and Turner product 4

24 2005 Fuller Smith and Turner product 3

45 2005 Fuller Smith and Turner product 5

20 2005 Noon products Ltd product 2

instead of what you have up there... weird ?
×

Success!

Help @lomokev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...