tags:

views:

67

answers:

2

Hi guys,

Suppose I have a table like this:

hseid | projcode | bulidarea | room | hall | floor | totalfloor | price

0 | 1 | 100 | 1 | 1 | 9 | 25 | 100

1 | 2 | 99  | 1 | 1 | 9 | 25 | 100

2 | 2 | 101 | 1 | 1 | 9 | 25 | 100

3 | 4 | 110 | 1 | 1 | 9 | 25 | 100

4 | 3 | 130 | 1 | 1 | 9 | 25 | 100

5 | 1 | 95  | 1 | 1 | 9 | 25 | 100

6 | 4 | 98  | 1 | 1 | 9 | 25 | 100

7 | 3 | 101 | 1 | 1 | 9 | 25 | 100

Note that hseid represented as unique building, projcode represents different property project developers, and the rest fields are not important.

Now to balance the argument among property project developers, I should make the result looks like this:

hseid | projcode | bulidarea | room | hall | floor | totalfloor | price

0 | 1 | 100 | 1 | 1 | 9 | 25 | 100

1 | 2 | 99  | 1 | 1 | 9 | 25 | 100

4 | 3 | 130 | 1 | 1 | 9 | 25 | 100

3 | 4 | 110 | 1 | 1 | 9 | 25 | 100

5 | 1 | 95  | 1 | 1 | 9 | 25 | 100

2 | 2 | 101 | 1 | 1 | 9 | 25 | 100

7 | 3 | 101 | 1 | 1 | 9 | 25 | 100

6 | 4 | 98  | 1 | 1 | 9 | 25 | 100

Basically the rule is:

  • Rotate each projcode
  • In each projecode, randomly pick one which is not already showed.

How do I implement this in MySQL?

Thanks a lot.

+1  A: 

I can't understand the rule but I think to transform the first table to the second one you can use Cursors in a procedure

Novemberland
A: 

I am not clear your rule or conditional field. However, if you are attempting to randomly select a project code based on showed, append the ORDER BY RAND() LIMIT 1 to your query. This will select the top one, with several assumptions about your table.

SELECT projcode FROM your_table WHERE showed_column = 0 ORDER BY RAND() LIMIT 1
Jason McCreary
Actually projcode field will not display randomly, but rotate (e.g. take a look the first table from top to bottom: 1 2 2 4 3 1 4 3 -> 1 2 3 4 1 2 3 4)
Ming Xie
Okay, I understand what you are doing now. The random selection included with the cycle make this difficult. Is it possible to use another technology on this? Or do you just have MySQL?
Jason McCreary
I have other 'tools' (e.g. PHP, Python, etc.) but also have massive amount of records (719,174 + 540,361 records). I think instead of reading all out, it's better to do it in MySQL.
Ming Xie
Agreed, but you have a very specific order and also a random order. I don't think you can accomplish that simply. I also think if you wrote a cursor it would be very messy. Take a look custom order clause: http://www.google.com/search?q=custom+order+mysql
Jason McCreary