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? 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 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" ; 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 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? 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)? 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 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.' 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. 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 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; 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." 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
Archived
This topic is now archived and is closed to further replies.