0

Does anyone know how to select those rows where x > y, where x and y both are table columns.

$query = mysql_query("SELECT * FROM table WHERE x > '???' ");
Henrikh
  • 130
  • 2
  • 3
  • 11

2 Answers2

3

To get all rows where the column x value is greater than that in column y it is this simple.

SELECT * /*BTW - Don't use *. List desired columns explicitly*/
FROM table 
WHERE x > y;
Martin Smith
  • 419,657
  • 83
  • 708
  • 800
  • why not to use *? for not to be harder to load? – Henrikh Sep 17 '11 at 15:06
  • @Daniel - Even if you need all columns now using `*` in your queries means that if you add new columns your queries may bring back unneeded data. See for example [discussion here](http://stackoverflow.com/questions/65512/which-is-faster-best-select-or-select-column1-colum2-column3-etc) – Martin Smith Sep 17 '11 at 15:09
2

You can specify column names anywhere a value would be valid, even on both sides of a relational operator.

SELECT *
  FROM table
  WHERE x > y
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325