ScrewLooseSalad Posted February 22, 2013 Share Posted February 22, 2013 I want to compare two columns, essentially this: $query = "SELECT * FROM table WHERE column3 < column4;"; Is this even possible? Or have I just got the syntax badly wrong? I've tried writing out a complicated php code breaking my task into three MySQL lookups instead but that's proving more trouble than its worth.. Everything I've found on Google hasn't been related to my problem... Is anyone able to help me in the right direction? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/ Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 Yes, it's possible. Why don't you post your actual code that you're having trouble with? Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414109 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 If you are running the query with mysql_query() in PHP then remove the ; after column4. Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414150 Share on other sites More sharing options...
ScrewLooseSalad Posted February 22, 2013 Author Share Posted February 22, 2013 Yes, it's possible. Why don't you post your actual code that you're having trouble with? I was hoping I wouldn't need it if I could only make that line of sql work Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414151 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 I was hoping I wouldn't need it if I could only make that line of sql work ... So .... why don't you post it HERE so we can HELP you make it WORK. Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414161 Share on other sites More sharing options...
ScrewLooseSalad Posted February 22, 2013 Author Share Posted February 22, 2013 (edited) ... So .... why don't you post it HERE so we can HELP you make it WORK. I did, $query = "SELECT * FROM table WHERE column3 < column4;"; the syntax for it is incorrect, I can't find anything similar on Google or in my book, and the best solution I can come up with involves looking up column3 and 4 separately, comparing them, then performing a third query to find all the relevant entries. Edited February 22, 2013 by ScrewLooseSalad Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414163 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 (edited) CREATE TABLE IF NOT EXISTS `temp` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `a_number` int(11) NOT NULL, `another_number` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; INSERT INTO `test`.`temp` ( `id` , `title` , `a_number` , `another_number` ) VALUES ( NULL , 'Row One', '1', '2' ), ( NULL , 'Row Two', '2', '1' ); SELECT * FROM `temp` WHERE a_number < another_number id title a_number another_number 1 Row One 1 2 If you want some help you're going to have to explain to us WHAT you are encountering. That query works. You haven't given us any information to help you, and it's annoying. (Sidebar: I used phpmyadmin for that dump, excuse the quoted numbers. Blah blah blah) Edited February 22, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414164 Share on other sites More sharing options...
Zane Posted February 22, 2013 Share Posted February 22, 2013 column3 and column4 MUST be in a numerical format like INT or FLOAT in order for that comparison to work. Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414165 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 column3 and column4 MUST be in a numerical format like INT or FLOAT in order for that comparison to work. Dates work too. Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414167 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 column3 and column4 MUST be in a numerical format like INT or FLOAT in order for that comparison to work. And so do varchars CREATE TABLE test ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, column3 VARCHAR(10), column4 VARCHAR(10) ); INSERT INTO test (column3, column4) VALUES ('aaaaa', 'bbbbb'), ('ccccc', 'bbbbb'), ('aaaaa', 'ccccc'), ('ddddd', 'bbbbb'); SELECT * FROM test WHERE column3 < column4; RESULTS--> 1, 2013-02-22 17:43:56, aaaaa, bbbbb 3, 2013-02-22 17:43:56, aaaaa, ccccc Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414176 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 Thnx Barand, I thought so too but didn't have the energy to add a few rows to my table. *coffee break time* Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414177 Share on other sites More sharing options...
fenway Posted February 23, 2013 Share Posted February 23, 2013 The comparison operator will always "work" -- but the implicit casting just might not do what you'd expect. Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414327 Share on other sites More sharing options...
Zane Posted February 24, 2013 Share Posted February 24, 2013 Never figured VARCHAR would do that. Learn something new everyday. Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414700 Share on other sites More sharing options...
fenway Posted February 24, 2013 Share Posted February 24, 2013 Never figured VARCHAR would do that. Learn something new everyday. That's how date strings are sorted, too (i.e. outside of mysql). Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414706 Share on other sites More sharing options...
ScrewLooseSalad Posted February 25, 2013 Author Share Posted February 25, 2013 thanks for your help, I've got it working now, I wasn't confident that my sql was correct Quote Link to comment https://forums.phpfreaks.com/topic/274814-comparison-query/#findComment-1414796 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.