[MySQL] Search in table on two columns RRS feed

  • Question

  • User-908043281 posted

    Search in table on two columns
    Hello, I need search in doTable on columns "month" and "title" the follow values:

    For column "month": Jan, Feb, Mar and Apr;
    For column "title": Latium, Abruzzo, Molise, Tuscany, Umbria, Campania and Sardinia.

    I tried this query but in the output I don't have one row where it is present in column title the string "Tuscany and Umbria".

    Can you help me?
    Thank you in advance.

    SELECT *
       month LIKE '%Jan%'
    OR '%Feb%'
    OR '%Mar%'
    OR '%Apr%'
    AND title LIKE '%Latium%'
    OR '%Abruzzo%'
    OR '%Molise%'
    OR '%Tuscany%'
    OR '%Umbria%'
    OR '%Campania%'
    OR '%Sardinia%';

    The doTable,  I need extract rows with ID number 1, 2 ad 5 :

    CREATE TABLE `doTable` (
      `title` longtext,
      `month` char(3) DEFAULT NULL,
      `id` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`id`)
    -- ----------------------------
    -- Records of doTable
    -- ----------------------------
    INSERT INTO `doTable` VALUES ('Umbria and Tuscany are two of Italy\'s most beautiful regions.', 'Jan', '1');
    INSERT INTO `doTable` VALUES ('Abruzzo, Latium and Molise National Park is famous in Italy,', 'Jan', '2');
    INSERT INTO `doTable` VALUES ('Hi, fairly new to you site so guessing this is a fairly basic question', 'Feb', '3');
    INSERT INTO `doTable` VALUES ('I have a question regarding the following question', 'Mar', '4');
    INSERT INTO `doTable` VALUES ('Sardinia is the second largest island in the Mediterranean Sea (after Sicily and before Cyprus) and an autonomous region of Italy', 'Mar', '5');
    INSERT INTO `doTable` VALUES ('I am a novice and I have uninstalled and reinstalled the community', 'Apr', '6');

    Tuesday, April 8, 2014 8:13 AM


  • User-802546231 posted

    does this solve your problem?

    SELECT *
       month IN ('Jan','Feb','Mar','Apr')
    AND (title LIKE '%Latium%'
    OR title LIKE '%Abruzzo%'
    OR title LIKE '%Molise%'
    OR title LIKE '%Tuscany%'
    OR title LIKE '%Umbria%'
    OR title LIKE '%Campania%'
    OR title LIKE '%Sardinia%');

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 18, 2014 9:40 AM