获取所有行,但将特定行放置在堆顶部

I have a bunch of domains that I store in a table. All of these domains point to the same root directory on the server. Hence each domain displays the exact same website. All of the domains stored in the table will be displayed in a list on the website. I will be looping through the fetched array to construct the HTML.

| SLD      | TLD | ... 
+----------+-----+-----+
| example  | com | ... 
| otherone | com | ...
| another  | org | ...
| fourth   | ai  | ...
| ...      | ... | ...

What I am going to do is emphasize the domain in the list of domains that corresponds to $_SERVER['HTTP_HOST']. For instance, if somebody visits example.com, example.com will be emphasized and placed at the top of the list on the page.

image of website

  • Is there a MySQL query that selects all rows, but is able to place one of the rows that matches a condition at index zero of the fetched array?

  • If not, other than nested loops, is there a way to select one of the rows or nested arrays whose values correspond to $_SERVER['HTTP_HOST'] at index zero?

God, I hope that makes sense.

Use a conditional sort and pass the current site as parameter @yourSite

 SELECT domain
 FROM YourTable
 ORDER BY CASE WHEN domain = @yourSite 
               THEN 0
               ELSE 1
          END, 
          domain

EDIT using concat.

 ORDER BY CASE WHEN CONCAT (SLD, '.', TLD) = @yourSite 
               THEN 0
               ELSE 1
          END, 
          CONCAT (SLD, '.', TLD)