Hello Developers,
How do I write Sql command for Mysql to add up number values of given columns and rank rows based on total values in ascending order ?
If there was only a single “keyword point” (kw1_point) to calculate, then I would have written the SQL like this using prepared statements:
$sql = "SELECT * from keywords WHERE kw1 = ? OR kw2 = ? OR kw3 = ? OR kw4 = ? order by kw1_point desc LIMIT 100";
Difficulty popsup when there are more than one column’s values to add up.
Example:
MySql table: keywords
“kw” - keyword.
id | kw1 | kw1_point | kw2 | kw2_point | kw3 | kw3_point | kw4 | kw4_point
----------------------------------------------------------------------------------------------
0 | mobile | 3 | phone | 3 | apps | 2 | tutorial | 2
----------------------------------------------------------------------------------------------
1 | mobile | 1 | phone | 1 | apps | 3 | tutorial | 3
----------------------------------------------------------------------------------------------
2 | tutorial | 3 | apps | 3 | mobile | 2 | phone | 3
----------------------------------------------------------------------------------------------
3 | mobile | 5 | tutorial | 5 | apps | 5 | usa | 5
----------------------------------------------------------------------------------------------
Glancing at each of the above rows points, we see this which is not in descending order:
id 0 = 10 points
id 1 = 8 points
id 2 = 11 points
id 3 = 20
NOTE: All 4 keywords exists on the first 3 matching rows.
However, only 3 words exist in the final matching row.
And the final matching row's keywords are not in the order of my keyword search either.
But, this should not matter.
Sql should ignore in which order the keywords are in on each column when comparing the order of my searched keywords. Sql should just:
**A). Find matching rows, regardless of how many of my searched keywords exist on each row;**
**B). Count the totals of each points, (count more than one column in this case), in each row; And**
**C) List the rows in the point's descending order.**
So, in this case, the SQL query should present rows in this descending order:
id | kw1 | kw1_point | kw2 | kw2_point | kw3 | kw3_point | kw4 | kw4_point
3 | mobile | 5 | tutorial | 5 | apps | 5 | usa | 5
2 | tutorial | 3 | apps | 3 | mobile | 2 | phone | 3
0 | mobile | 3 | phone | 3 | apps | 2 | tutorial | 2
1 | mobile | 1 | phone | 1 | apps | 3 | tutorial | 3
Calculating each of the above rows points, we get rows ranked in this order:
id 3 = 20
id 2 = 11 points
id 0 = 10 points
id 1 = 8 points
These following SQL commands do not work:
I was suggested the following but it only presents me with 1 matching row (**id:0**) even though there exist other matching rows in my mysql tbl.
**SQLs**
Exact match
SELECT * from keywords WHERE kw1 = ? OR kw2 = ? OR kw3 = ? OR kw4 = ? ORDER BY (kw_1_point+kw_2_point+kw_3_point+kw_4_point) DESC
Fuzzy match:
SELECT * from keywords WHERE kw1 LIKE ? OR kw2 LIKE ? OR kw3 LIKE ? OR kw4 LIKE ? ORDER BY (kw_1_point+kw_2_point+kw_3_point+kw_4_point) DESC