Jump to content

comparison query


ScrewLooseSalad

Recommended Posts

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

Link to comment
Share on other sites

... 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 by ScrewLooseSalad
Link to comment
Share on other sites

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 by Jessica
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.