tags:

views:

33

answers:

3

Hi,

I need some help with a query.

i want to select from a table, some values, but the values depend on the value of an other cell. after i select i need to sort them.

Can i use ELECT column FROM table WHERE one=two ORDER BY ...?

thanks, Sebastian

+3  A: 

Yes you can, as long as you spell SELECT correctly.

Here is an example you can copy and paste into your MySQL Query Browser to see a query of this type in action:

CREATE TABLE table1 (
    id INT NOT NULL,
    name1 VARCHAR(100) NOT NULL,
    name2 VARCHAR(100) NOT NULL,
    sortorder INT NOT NULL
);

INSERT INTO table1 (id, name1, name2, sortorder) VALUES
(1, 'Foo', 'Foo', 4),
(2, 'Boo', 'Unknown', 2),
(3, 'Bar', 'Bar', 3),
(4, 'Baz', 'Baz', 1);

SELECT id
FROM table1
WHERE name1 = name2
ORDER BY sortorder;

Result:

4
3
1
Mark Byers
A: 

Maybe some working examples will help:

This returns over 8100 records from one of my databases:

SELECT * FROM fax_logs WHERE fee = service_charge

This returns over 2700 records from my data:

SELECT * FROM fax_logs WHERE fee = service_charge + 5

This returns over 6900 records:

SELECT * FROM fax_logs WHERE fee = service_charge + copies

Joshaven Potter
A: 

I might misunderstood your question, but I think you are trying to compare values of the first and second column. In Mysql, you can refer columns by number, not by name, only inside ORDER BY clause: SELECT * FROM table ORDER BY 1 (order by the first column). You cannot use column index in WHERE.

a1ex07