How to ORDER BY which ever Parameter comes first in the URL or MySQL query?

mysql select where
write a query to display the name of the first customer and the last customer who ordered products
mysql select example
mysql offset
mysql order by
sql join
mysql query syntax
write a query to display the names of customers who have ordered more than 2 times.

When I run the query, it keeps grouping tags in a random order.

This is how it looks currently:

https://www.example.com/app/user-images.php?tags=peace&tags2=dogs&tags3=aesthetic

Image #1 - peace

Image #2 - peace

Image #3 - dogs

Image #4 - dogs

Image #5 - aesthetic

Image #6 - peace

Expected Result:

Image #1 - peace

Image #2 - peace

Image #3 - peace

Image #4 - dogs

Image #5 - dogs

Image #6 - aesthetic

Is there a way I can possibly do this? First tag would be peace, second dogs, third aesthetic based on the URL. Here is my code:

$tags = $_GET['tags'];
$tags2 = $_GET['tags2'];
$tags3 = $_GET['tags3'];

$fetch = mysqli_query($conn, "SELECT * FROM users JOIN user_images ON users.username = user_images.username WHERE tags = '$tags' OR tags = '$tags2' OR tags = '$tags3' ORDER BY user_images.tags ASC"); 

I tried using ORDER BY user_images.tags ASC but that doesn't really help since no matter where I put the tags in the URL, they all come the same order.

You can sort by multiple criteria, so in this case adding your search terms to the ordering should work. E.G:

...
ORDER BY tags = '$tags' DESC, tags = '$tags2' DESC, tags = '$tags3' DESC

MySQL: how to fetch the Nth segment of a URL, into its parts. A simple MySQL query to aid you when doing website analysis. First, we need to return the part of the URL that ends with the segment we need. And next Here is how to fetch the Nth segment in a URL in MySQL: First, use� 2 How to ORDER BY which ever Parameter comes first in the URL or MySQL query? Feb 16 '19. 1 Need to read a register repeatedly using mmap(). Read value does not

The first idea that comes to mind is to do 3 separate queries, one for each tag:

$results = [];
foreach ([$tags, $tags2, $tags3] as $tag) {
    $fetch = mysqli_query($conn, "SELECT * FROM users JOIN user_images ON users.username = user_images.username WHERE tags = '$tag'");
    while ($row = $fetch->fetch_assoc())
        $results[] = $row;
    $fetch->close();
}

While this code probably works, it leaves a wide open door for SQL injection. You should use positional parameters inside the query and pass the actual values outside of the query to prevent that.

MySQL 8.0 Reference Manual :: 13.2.10 SELECT Statement, SELECT is used to retrieve rows selected from one or more tables, and can include must come after any GROUP BY clause and before any ORDER BY clause. With two arguments, the first argument specifies the offset of the first row to� No, that order doesn't matter (or at least: shouldn't matter). Any decent query optimizer will look at all the parts of the WHERE clause and figure out the most efficient way to satisfy that query. I know the SQL Server query optimizer will pick a suitable index - no matter which order you have your two conditions in.

You can use a CASE expression to control your ordering, giving priority to each tag in order:

SELECT * 
FROM users 
JOIN user_images ON users.username = user_images.username 
WHERE tags = '$tags' OR tags = '$tags2' OR tags = '$tags3' 
ORDER BY CASE WHEN tags = '$tags' THEN 1
              WHEN tags = '$tags2' THEN 2
              WHEN tags = '$tags3' THEN 3
              END

QUERYING THE DATABASE: QUERIES and VIEWS, Run the Orders Query (Orders Qry on the Query list): It lists all orders for all This is the SQL statement, separated into logical sections for ease of interpretation: Access, MySQL, and Oracle all use similar syntax, with more join types and options customer's name and address once only (i.e., remove redundant columns). The find.php file doesn't know or care that its receiving its instructions from a jQuery function. As far as it's concerned, the search term comes from an ordinary form submit process, and it uses that data to complete a query, then return the records that match that term.

ORDER BY Clause (Transact-SQL), That is, the result set is sorted by the first column and then that ordered list is sorted by the second column, and so on. using the OPTIMIZE FOR query hint for the OFFSET and FETCH parameters. Address AS a ON a. The next thing is to write a query to extract the required data from the database. First set up the query as required, then apply a filter from the drop down box (1 below) for any random date (2 below). This creates a query step as shown below – the query step directly references the date selected above.

mysql_query - Manual, See also MySQL: choosing an API guide and related FAQ for more information. The following query is syntactically invalid, so mysql_query() fails and returns FALSE . $query = sprintf("SELECT firstname, lastname, address, age FROM friends you get the enum from the first, so be careful with the $Column argument Except that this time we can’t take advantage of query folding. To pass a parameter to the database, we have to separate it from the parameters in order to avoid the formula firewall. This means that we break query folding. And this means that Power Query needs to pull in all 40 million records, and process them.

MySQL: ORDER BY Clause, This MySQL tutorial explains how to use the MySQL ORDER BY clause with The MySQL ORDER BY clause is used to sort the records in your result set. The ORDER BY clause can be used in a SELECT statement, SELECT LIMIT to sort by relative position in the result set, where the first field in the result set is 1. Set the environment variables MYSQL_DATABASE, MYSQL_HOST, MYSQL_PORT, MYSQL_USER and MYSQL_PASSWORD. MYSQL_SOCKET can also be used in place of MYSQL_HOST and MYSQL_PORT to connect over a UNIX socket. Then run npm test. For example, if you have an installation of mysql running on localhost:3306 and no password set for the root user, run:

Comments
  • order by tags = '$tags', tags = '$tags2', tags = '$tags3'
  • @Moob Thank you, this almost work but for some reason it's going backwards. The third tag is going first and the first is going third.
  • Sort descending. Eg: order by tags = '$tags' desc, tags = '$tags2' desc, tags = '$tags3' desc
  • Hey @Moob : this is a nice solution, I believe that you should post it as an answer.
  • @GMB You're right. I've posted my solution as an answer.
  • much apperciated, but I got this error in the code: Warning: array_merge(): Argument #1 is not an array. I've never seen array_merge, it seems interesting.
  • @radu a UNION would work here.
  • @LaneyWilliams You are right, mysqli_query() returns an object, so you have to extract the results before you can merge them. I have edited my answer and replaced the code with something that actually works. I apologize for not checking it in the first place.
  • @Moob I totally agree a "UNION" works too. Performance is always debatable, but sometimes multiple simple queries are faster than a single more complex query. In this case, all results need to be fetched into PHP eventually. On the other hand, if a limit/offset is needed on the result, then the UNION is definitely better.