How to sort items in mysql based on data from another table?

Neither of those answers from Denis or Johan are correct. Instead you could use this.

Neither of those answers from Denis or Johan are correct. Instead you could use this: select itemid, sum(word_importance) as item_importance from (select itemid, search. Wordid, ( in_title * (importance + 1) + in_description * importance ) as word_importance from items_word_match, search where i.

Wordid = s. Wordid ) group by itemid As Johan pointed out, you need to add an order clause to the end, , order by item_importance desc.

1 your query doesn't SORT by importance, add an order by clause because the implicit ordering in the group by is the wrong one. BTW I wasn't trying to answer the question in the body, but answering the question in the title in general. Nice looking query though.

– Johan May 22 '11 at 20:20.

You can sort the outcome of a query by any criterium you wish. SELECT word_list. * FROM word_list INNER JOIN search ON (search.

Wordid = wordlist. Id) ORDER BY search. Importance, word_list.

Id DESC Note that the JOIN needed to link the two tables together can have a profound effect on which rows in the word_list table are selected, but you need to do a JOIN somehow. Otherwise MySQL will not know what the relationship between the two tables is and cannot sort the fields.

SELECT i. Itemid , SUM( i. In_description * s.

Importance + i. In_title * ( s. Importance + 1 ) ) AS item_importance FROM items_word_match I LEFT JOIN search s ON s.

Wordid = i. Wordid GROUP BY i. Itemid ORDER BY item_importance DESC CORRECTION: I used LEFT JOIN catch the case when some words do not appear in the search table.

But then the importance of those words seems appropriate to be 0 and not NULL, so the SUM should be changed into: , SUM( i. In_description * COALESCE(s. Importance, 0) + i.

In_title * COALESCE(s. Importance, 1) ).

1 best answer, note that you can shorten the query by saying ORDER BY item_importance instead of ORDER BY SUM(.... – Johan May 22 '11 at 20:25 @Johan: Yes, thnx. I keep forgetting that because you can't use an alias in another calculated field. But you can in ORDER BY.

– ypercube May 22 '11 at 20:29.

Your order by clause can contain fields from any of the tables: select table1. * from table1 join table2 using (table1_id) order by table2. Field, table1.

Field, etc.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions