Seleccionar solo una subcadena en MySQL y luego devolverla [duplicada]

Posible duplicado:
mysql_fetch_array () espera que el parámetro 1 sea recurso, booleano dado en select

In MySQL I am trying to select only the distinct years from over a thousand rows using a textual field called 'edate' that is formatted "1-Jan-2010":

SELECT DISTINCT DATE_FORMAT(STR_TO_DATE(edate, '%e-%b-%Y'), %Y) 
  FROM addnews 
 WHERE status = 1 
   AND archives = 1 
   AND delete1 = 0

Devoluciones de MySQL

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\WAMP\www\baycrossings\web\archives.php on line 28"

In MS SQL this works as follows:

select distinct(year(edate)) 
  from addnews 
 where status = 1 
   and archives = 1 
   and delete1 = 0

Does MySQL not allow substring selection? I am trying to avoid selecting hundreds of times more data than I need per query.

preguntado el 28 de agosto de 11 a las 03:08

is there a special reason you are not using the date time column type in mysql? -

3 Respuestas

Did you forget to quote the %Y segundo argumento para DATE_FORMAT()?

Try changing your query call to look like this:

$result = mysql_query($your_query) or trigger_error(mysql_error());

That way you can see the actual error that MySQL is returning, as opposed to just an error about mysql_query() returning FALSE (which is what the error you pasted is saying).

Beyond that, you should not be storing dates as strings in the database. The native date field types should be used, as they allow date 'math' to be peformed without having to do some very expensive string-to-date conversion.

Since you're just trying to extract a year from a string date, assuming the date format is constant, try using:

SELECT ..., RIGHT(edate, 4)

which will be a far faster operation than a string-to-date-to-string round trip operation.

Respondido 28 ago 11, 08:08

The database was given to me this way, with thousands of dates formatted as varchar(50). Perhaps there is a MySQL command to reformat them into a date format... Thank you. - limedrop

If this (or another) answer allowed you to solve your problem, consider clicking the green outline of a checkmark to the left of it to mark it as Accepted. - Amber

Is there a reason you're not just using the YEAR function in MySQL as well?

Anyway, the problem with your current query is that you need to put the %Y in single quotes, like this:

SELECT DISTINCT DATE_FORMAT(STR_TO_DATE(edate, '%e-%b-%Y'), '%Y') 
FROM addnews WHERE status=1 AND archives=1 AND delete1=0

Respondido 28 ago 11, 07:08

Wow, the single quotation mark wins! Thank you. P.S. The way the date is formatted is not the way MySQL understands dates if I understand correctly. Anyway YEAR(edate) did not work for me. - limedrop

i meant year(str_to_date()) instead of dateformat(str_to_date(),'%Y'). no biggie, glad we could help - Derek Kromm

If you get that "boolean" error with a MySQL operation, it generally means your query failed, and the PHP query function has returned a boolean false. Your code should look like this:

$sql = "...";
$result = mysql_query($sql) or die(mysql_error());

If your query is invalid (syntax error, fails constraints, etc...), this construct will kill your script and output the exact reason for the error. Without the or die(), your could would take the boolean false that msyql_query() returns, and attempt to do another operation (fetch?), which now also fails, because you can't fetch a row from a "FALSE".

Respondido 28 ago 11, 08:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.