I have two tables. First table looks like this:
Table: Records
rid | user id | title | whenadded | public -------------------------------------------------------------------- 1 212 Example 2012-06-28 1 2 217 Test Rec 2012-07-05 1 3 212 Another 2012-07-02 1 4 212 Unlisted 2012-05-02 0 5 217 Success 2012-04-08 1 6 238 Always 2012-04-18 1
Table: Likes
id | user id | rid | whenliked ------------------------------------------------------ 1 212 2 2012-07-06 2 205 1 2012-06-30 3 212 5 2012-07-04
In the 'Records' table, 'rid' is set as the primary index. In the 'Likes' table, id is set as the primary index.
I'm using PHP. PHP will provide a value to MySQL to use as reference. I'd like to have a single MySQL query that will do the following:
Pseudo Code:
$userid = 212;
$SQL = 'SELECT DISTINCT records.*
FROM records,likes
WHERE (records.userid = ' . $userid .
' AND records.public = 1)
OR (records.id = likes.rid AND likes.userid = ' . $userid .
' AND records.public = 1)
ORDER BY likes.whenliked DESC, records.whenadded DESC
LIMIT 50;';
Look at the $SQL query I just provided above. That's my attempt at developing the query I wanted, but it didn't achieve what I was looking for; it came pretty damn close, but it was still ordering incorrectly. The query was developed as best as I could first by myself, then based on what I found by searching for a solution on StackFlow and elsewhere on Google.
Those are the conditions I'm trying to order it by:
The end result from the query returned would look like this (keep in mind, whenliked is NOT in the return data, it's just there for reference so you see how it's ordered):
rid | user id | title | whenadded | public | whenliked {not incl} ------------------------------------------------------------------------------------ 2 217 Test Rec 2012-07-05 1 2012-07-06 3 212 Another 2012-07-02 1 5 217 Success 2012-04-08 1 2012-06-30 1 212 Example 2012-06-28 1
Hope that makes sense. Feel free to ask questions, I'll clarify the best I can.
Thanks in advance for your time, your consideration, and for reading this. Even if there is no response or no solution is found, your time is still very much appreciated! :)
If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY . This clause comes at the end of your SQL query. After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary).
You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.
Syntax: SELECT * FROM table_name ORDER BY column_name; For Multiple column order, add the name of the column by which you'd like to sort records first. The column that is entered at first place will get sorted first and likewise.
Column order does not matter while creating a table. We can arrange the columns while retrieving the data from the database. Primary key can be set to any column or combination of columns.
The field you are ordering by doesn't have to be an existing field in the database. You can also use a field you define in your select:
SELECT IF(records.userid = ' . $userid . ', records.whenadded, likes.whenliked) as date
Now you can use it in the order by part:
ORDER BY date DESC
From the MySQL Manual:
A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With