user1401469
user1401469

Reputation: 25

Order by column in another table PHP

I have tables like this

COUNT tbl id, userid, linkid, count

                         4
                         1
                         6

LINKS tbl

id,  linkname,  linkurl

What i want to do is to order the 'linkname' column in order of the count column and put it in an array. I am struggling becasue i am not understanding how to use JOIN.

I need to get linkid WHERE userid = $userid

Upvotes: 1

Views: 704

Answers (2)

Jeff Lambert
Jeff Lambert

Reputation: 24661

Try this:

$userid = intval( $userid ); // Hopefully it's already an integer, 
                             // but protect yourself from SQL Injection

SELECT linkname, C.count FROM Links INNER JOIN `Count` C ON C.linkid = Links.id
WHERE userid = $userid
ORDER BY C.count ASC

Upvotes: 1

ametren
ametren

Reputation: 2246

Try this: SELECT * FROM Links L JOIN Count C ON L.id = C.linkid then you should have a count column.

Also, I recommend you don't use "count" as the name of a table since it is a SQL reserved word.

Upvotes: 0

Related Questions