Question: sqlite/php - order by 'input order' - with imploded query

Question

sqlite/php - order by 'input order' - with imploded query

Answers 1
Added at 2016-12-19 08:12
Tags
Question

Dollar.

$qtwo = str_split_unicode($q); 
$arrlength = count($qtwo);

and then I got:

$where = array();
foreach ( $qtwo as $word ) {
$where[] = "CHS LIKE '%" . $word . "%'";
}
$where = implode(' OR ', $where);    

if ($q[0] == "#")  {
} else {$results4 = $db->query("SELECT * FROM FOUR WHERE $where");
while ($row4 = $results4->fetchArray()) {

and then a bunch of echoing etc - but the results come back in alphabetical order - first come first served, e.g.:

隔山买老牛

comes back as

——————————————————————————

《现代汉语方言音库 • 成都话音档》

词语: 隔

发音: ge2

国际音标: ke²¹

——————————————————————————

《现代汉语方言音库 • 成都话音档》

词语: 买

发音: mai3

国际音标: mai⁵³

——————————————————————————

《现代汉语方言音库 • 成都话音档》

词语: 老

发音: lao3

国际音标: nau⁵³

——————————————————————————

《现代汉语方言音库 • 成都话音档》

词语: 牛

发音: niu2

国际音标: ȵiəu²¹

——————————————————————————

《现代汉语方言音库 • 成都话音档》

词语: 山

发音: san1

国际音标: san⁵⁵

For anyone who finds Chinese kind of foreign it'd be like searching: GSMLN and getting the results in alphabetical order: GMLNS.

My goal is to get the (output) order the same as the input (order).

Is there a simple order by I can use? or does it get more complicated?

Answers
nr: #1 dodano: 2016-12-19 10:12

In SQL, tables always are unordered; there is no guarantee that a query uses any specific order.

If you want to get the rows in a specific order, you have to explicitly sort them by using ORDER BY in the query.

If there isn't some table data that you can use directly for sorting, you have to use a CASE expression to map the table values to some values usable for sorting:

SELECT ...
ORDER BY CASE WordColumn
         WHEN '隔' THEN 1
         WHEN '山' THEN 2
         WHEN '买' THEN 3
         WHEN '老' THEN 4
         WHEN '牛' THEN 5
         END;
Source Show
◀ Wstecz