AndyPSV Posted December 11, 2014 Share Posted December 11, 2014 I've got. SELECT * FROM '.PRFX.'sell WHERE draft = "0" AND id IN (SELECT id_ FROM '.PRFX.'skipped WHERE uid = "'.$u.'") And want to order by ID field (DESC) from '.PRFX.'skipped' How to do it? Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/ Share on other sites More sharing options...
Barand Posted December 11, 2014 Share Posted December 11, 2014 (edited) Use a JOIN to find the matching records instead of IN (SELECT ...) $tblsell = PRFX.'sell'; $tblskipped = PRFX.'skipped'; $sql = "SELECT * FROM $tblsell AS sel INNER JOIN $tblskipped AS sk ON sel.id = sk.id_ AND sk.uid = $u WHERE sel.draft = 0 ORDER BY sk.id" ; Edited December 11, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499296 Share on other sites More sharing options...
AndyPSV Posted December 11, 2014 Author Share Posted December 11, 2014 Found already an answer. SELECT DISTINCT * FROM '.PRFX.'sell sell JOIN '.PRFX.'skipped skip ON skip.id_ = sell.id WHERE draft = "0" AND skip.uid = "'.$u.'" ORDER BY skip.id DESC Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499297 Share on other sites More sharing options...
AndyPSV Posted December 11, 2014 Author Share Posted December 11, 2014 I've got. SELECT DISTINCT * FROM '.PRFX.'sell sell JOIN '.PRFX.'followed follow ON follow.id_ = sell.id WHERE draft = "0" AND follow.uid = "'.$u.'" ORDER BY follow.id DESC '.$sql_limit that orders table, but it messes with the original '.PRFX.'sell ID (exchanging it with the follow.id) How to make it back to the original one? Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499302 Share on other sites More sharing options...
AndyPSV Posted December 11, 2014 Author Share Posted December 11, 2014 Examples mess my original ID (from sell table) and i MUST preserve it. How to do it (Still waiting for an answer)? Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499303 Share on other sites More sharing options...
Barand Posted December 11, 2014 Share Posted December 11, 2014 1. I am not on your payroll and therefore do not respond well to comments like Still waiting for an answer 2. I haven't a clue what you are talking about when you say Examples mess my original ID Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499305 Share on other sites More sharing options...
AndyPSV Posted December 11, 2014 Author Share Posted December 11, 2014 I want to preserve ORIGiNAL sell.id instead of id: '1 2 3 4 etc.' Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499306 Share on other sites More sharing options...
Barand Posted December 11, 2014 Share Posted December 11, 2014 It's a SELECT query. It is not changing any data in your database. Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499309 Share on other sites More sharing options...
AndyPSV Posted December 11, 2014 Author Share Posted December 11, 2014 Yeah, but I get '1 2 3 4' IDs instead of original sell.id's i.e. 12 14 16 19 Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499311 Share on other sites More sharing options...
AndyPSV Posted December 11, 2014 Author Share Posted December 11, 2014 It's done. SELECT DISTINCT sell.*, follow.id as followidFROM '.PRFX.'sell sell JOIN '.PRFX.'followed followON follow.id_ = sell.idWHERE draft = "0" AND follow.uid = "'.$u.'"ORDER BY follow.id DESC '.$sql_limit; Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499314 Share on other sites More sharing options...
Barand Posted December 11, 2014 Share Posted December 11, 2014 The moral is "Don't use 'SELECT * '. Specify the columns you want." Quote Link to comment https://forums.phpfreaks.com/topic/293036-how-to-order-by-order-in-other-table/#findComment-1499315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.