Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 09-24-2005, 05:01 AM mysql question
Experienced Talker

Posts: 38
Location: Holland
Trades: 0
I am stuck with something, i was wondering if you could help me out
i have a mysql table like this:

id, name, class, type

1 - n1 - table - large
2 - n2 - table - small
3 - n3 - chair - middle
4 - n4 - table - large
5 - n5 - table - small
6 - n6 - chair - middle
etc etc.. lot of records

now i want to display it like this:

[class table] - [type small]
here all the small tables

[class table] - [type large]
here all the large tables

[class chair] - [type middle]
here all the middle chairs

etc etc..


i was wondering how i could do this on an easy way? i could make a query for every kind of course but this will have a lot of querys, that i rather dont do..
is there a way to solve this easy?

greets ex
excalibur112 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-24-2005, 07:58 AM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
SELECT fieldlist FROM table ORDER BY class ORDER BY type DESC;

Will work except for the type will sort in alphabetical order only so if you change the data in the field or capitalise an entry accidentally you will have sorting problems.

You should normalise the database tables to remove the need to have "large", "middle" & "small" make into a linked table and store the id for type in the main table.
It will reduce the table sizes by 50% on average and make queries faster
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 09-24-2005 at 07:59 AM.. Reason: corrected "fat finger" typing
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-24-2005, 08:16 AM
Experienced Talker

Posts: 38
Location: Holland
Trades: 0
hey thanx for your answer,

to be honiced i have 2 tables like you explained, but i thought lets write an easy preview, so it would be easyer to understand.

i understand what you mean, with order by, i was thinking about that too.
But i only know the order by with count etc..

I dont understand how to display ordered groups.

like if you do

PHP Code:
while($row mysql_fetch_assoc($sql)) {

//how you display one group here?

//one group here

//etc..

excalibur112 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2005, 02:36 PM
Skilled Talker

Posts: 62
Trades: 0
It might be a good time to take a step back from the issue at hand and consider redesigning your database - the purpose of database architecture is to reduce redundancy as much as possible.

Consider the following 3 tables to replace your current setup:

Table: items
item_id - item_type_id - item_size_id - item_name

1 - 1 - 3 - n1
2 - 1 - 1 - n2
3 - 2 - 2 - n3
4 - 1 - 3 - n4
... etc ...

Table: items_type
item_type_id - item_type_name

1 - Table
2 - Chair
... etc ...

Table: items_size
item_size_id - item_size_name

1 - Small
2 - Middle (Medium?)
3 - Large

Now for the fun queries...

Select all items, order by item type then item size then item name:
Code:
SELECT
items.item_name,
items_type.item_type_name,
items_size.item_size_name

FROM

items,
items_type,
items_size

WHERE

items.item_size_id = items_size.item_size_id

AND

items.items_type_id = items_type.item_type_id

ORDER BY

items.item_type_id,
items.item_size_id,
items.item_name
Want just one type of item?
Code:
SELECT
items.item_name,
items_type.item_type_name,
items_size.item_size_name

FROM

items,
items_type,
items_size

WHERE

items.item_size_id = items_size.item_size_id

AND

items.item_type_id = items_type.item_type_id

AND

items.item_type_id = 'X'

ORDER BY

items.item_type_id,
items.item_size_id,
items.item_name
(Where 'X' is the id# of the type of item you'd like to select - alternately you could select "AND items_type.item_type_name='Table'" but the comparison would take slightly longer)
__________________

Please login or register to view this content. Registration is FREE
danlefree is offline
Reply With Quote
View Public Profile Visit danlefree's homepage!
 
Reply     « Reply to mysql question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13816 seconds with 12 queries