Jump to content

Recommended Posts

Hi!

 

I've a table 'books' with this data:

yeardiscipline

pré-escolargeometry

1math

1english

 

I'm trying to do a mysql query to return the distinct disciplines with  the year 'pré-escolar' and '1'. I've tried with

SELECT distinct `discipline` FROM `books` WHERE `year`="Pré-escolar" OR `year`="1"

 

But it only returns 'geometry'.

 

Can someone help me? Thx.

Link to comment
https://forums.phpfreaks.com/topic/242617-query-with-number-or-text/
Share on other sites

Why not

SELECT distinct `discipline` FROM `books` WHERE `year`='Pré-escolar' OR `year`='1'

?

 

If that will not work, try:

 

SELECT distinct `discipline` FROM `books` WHERE `year`='Pré-escolar' 
UNION
SELECT distinct `discipline` FROM `books` WHERE  `year`='1'

 

But really, using the WHERE ...OR.. version should suffice. But the two queries are logically equivalent, so either of them should do.

 

For current MySQL implementation, maybe the first version will fair well, performance wise.

Thank you for the quick reply, but i'm getting the same result with both of your suggestions.

 

I've more rows on that table with more years. If i make this query only with numbers, it returns the disciplines of both years.

SELECT distinct `discipline` FROM `books` WHERE `year`='1' OR `year`='2'

 

But with 'pré-escolar' and a number it doesn't work.

I'm not an expert in this matter, but the query only returns the proper results this way.

 

SELECT distinct `discipline` FROM `books` WHERE `year`="1" OR `year`="Pré-escolar"

Only returns the discipline of of 'pré-escolar'.

 

SELECT distinct `discipline` FROM `books` WHERE `year`=1 OR `year`=Pré-escolar

Shows an SQL error: #1054 - Unknown column 'Pré' in 'where clause'

 

SELECT distinct `discipline` FROM `books` WHERE `year`=1 OR `year`="Pré-escolar"

Works!

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.