Jump to content
php.lv forumi

order by


yeahz

Recommended Posts

ir šāda tabula

 

+----+------+-------+
| id | plus | minus |
+----+------+-------+
| 1  | 8    | 3     |
+----+------+-------+
| 2  | 1    | 2     |
+----+------+-------+
| 3  | 3    | 6     |
+----+------+-------+
| 4  | 1    | 1     |
+----+------+-------+
| 5  | 2    | 4     |
+----+------+-------+

 

vai ir iespējams izveidot ORDER BY tā, lai viņš rēķinātu pēc plus-minus (no plusa atņem mīnusu)? tātad lai sanāk šādi:

 

+----+------+-------+
| id | plus | minus |
+----+------+-------+
| 1  | 8    | 3     |    jo 8-3=5
+----+------+-------+
| 4  | 1    | 1     |    jo 1-1=0
+----+------+-------+
| 2  | 1    | 2     |    jo 1-2=-1
+----+------+-------+
| 5  | 2    | 4     |    jo 2-4=-2
+----+------+-------+
| 3  | 3    | 6     |    jo 3-6=-3
+----+------+-------+

 

centos šādi:

 

SELECT * FROM table ORDER BY plus-minus DESC

 

taču šādā gadījumā viņš izvilka kaut ko pilnīgi bezsakarīgu.

Edited by yeahz
Link to comment
Share on other sites

Viss strādā šai gadījumā. Iespējams, ka datu tips tabulā nav vis skaitliskais, piemēram, INT, bet simboliskais?


mysql> create table q (id int, plus int, minus int);
Query OK, 0 rows affected (0.09 sec)

mysql> insert into q values (1, 8, 3), (2, 1,2), (3, 3, 6), (4, 1, 1), (5, 2, 4)
;
Query OK, 5 rows affected (0.05 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from q order by plus-minus desc;
+------+------+-------+
| id   | plus | minus |
+------+------+-------+
|    1 |    8 |     3 |
|    4 |    1 |     1 |
|    2 |    1 |     2 |
|    5 |    2 |     4 |
|    3 |    3 |     6 |
+------+------+-------+
5 rows in set (0.02 sec)

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

lauki plus un minus tiek veidoti šādi:

 

LEFT JOIN (SELECT count(*) AS count_pluses, article_id FROM (SELECT article_id FROM rating WHERE rating='+') AS rating GROUP BY article_id) AS r_plus ON r_plus.article_id=a.id
LEFT JOIN (SELECT count(*) AS count_minuses, article_id FROM (SELECT article_id FROM rating WHERE rating='-') AS rating GROUP BY article_id) AS r_minus ON r_minus.article_id=a.id

 

tabulas rating struktūra:

 

+----+------------+--------+
| id | article_id | rating |
+----+------------+--------+
| 1  | 1          | +      |
| 2  | 1          | -      |
+----+------------+--------+

Edited by yeahz
Link to comment
Share on other sites

Atlasi arī to izteiksmi pēc kuras kārto.

Man arī šādi viss notiek:

mysql> SELECT article_id, plus, minus, plus-minus
   -> FROM (
   ->   SELECT COUNT(CASE WHEN rating = '+' THEN 1 ELSE NULL END) as plus,
   ->          COUNT(CASE WHEN rating = '-' THEN 1 ELSE NULL END) as minus,
   ->          article_id
   ->   FROM ratings
   ->   GROUP BY article_id) as q
   -> ORDER BY plus-minus DESC;
+------------+------+-------+------------+
| article_id | plus | minus | plus-minus |
+------------+------+-------+------------+
|          2 |    2 |     0 |          2 |
|          1 |    3 |     2 |          1 |
|          3 |    1 |     4 |         -3 |
+------------+------+-------+------------+
3 rows in set (0.00 sec)

un versija - tas varētu būt svarīgi:
mysql> show variables LIKE 'version';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| version       | 5.1.52-community |
+---------------+------------------+
1 row in set (0.02 sec)

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...