necesita una consulta sql

I have a table with the schema below. I need to fetch the last 12 icons that contain images and caricature and ....

I need the last 12 fields that contains images and the last 12 fields that contains caricature. Meaning the last 24 field that contains images or caricature

| id   | icons               |
| 2583 | images              |
| 2582 | images              |
| 2581 | images,caricature   |
| 2580 | economic            |
| 2579 |                     |
| 2578 |                     |
| 2577 |                     |
| 2576 |                     |
| 2575 | images,audio        |
| 2574 | images,infographics |

preguntado el 09 de marzo de 12 a las 14:03

You need to reword this question, it's incredibly unclear. Also, are you currently designing this table? If so, the icons strings should be a separate table. -

Do you have any source tables? Please post them. What have you tried so far? -

Improve your table structure first, and normalize the icons field. Storing csv-type data in a single field defeats the purpose of having a relational database. Once you've fixed that up, then you'll have a "top N per group" type query, which has been answered on this site many times before. -

Two things: one, what do you mean by last? I suppose you meant the más alto id campo, but then again I can't tell, mainly because of 2) your ... ellipses. The first one makes me think you were trying to "start over fresh", and restate the question. The second one makes me wonder if you're assuming the reader will "fill in the blanks" with the content listed previously in the question. Can you please elaborate, and give an example set of output from this query? -

Keep the table in the question. Please don't link to a graphic that shows its structure. -

3 Respuestas

If you're looking for 12 items per each of the 3 categories, then only one limit 12 clause clearly won't solve it. What you're trying to do is to UNION the top 12 results of those 3 categories. So you you should try this:

(select id, 'caricature' from t
where icons like '%caricature%'
order by id desc
limit 12)
(select id, 'economic' from t
where icons like '%economic%'
order by id desc
limit 12)
(select id, 'images' from t
where icons like '%images%'
order by id desc
limit 12)

Note: You should not remove the parentheses.

Avísame si tienes alguna duda.

respondido 09 mar '12, 15:03

i need to fetch 7 items, as a result your solution do 7 query.i think this solution is not good enough for performance issue. - na3r

You need to apply a filter per each of the "queries" how can you get 12 items per each item type without limiting each of them to 12? - mosty mostacho

select top 12 * from yourTable where Icons like '%caricature%' ORDER BY ID DESC

then do the same for whatever else condition in the icons field.

respondido 09 mar '12, 14:03

i got this error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12 * from news where Icons like '%caricature%' ORDER BY ID DESC LIMIT 0, 30' at line 1 - na3r

@na3r: You didn't specify that you are using MySQL in the first place. Use Limit 12 en lugar de top 12 and put it at the end of your query. - juergen d

puedes hacer esto usando like operator y límitar

select * 
from your_table
where icons like '%caricature%' and icons like '%images%' and ...
order by id desc
limit 12

respondido 10 mar '12, 11:03

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