Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:27:29+00:00 2026-05-28T03:27:29+00:00

I have a table with fields id , text , and title . There

  • 0

I have a table with fields id, text, and title. There may be multiple rows with the same title (title is not unique). Additionally, id is the primary key.

I need only to know if there are at least one row with title=”A” that contains the string “aaa” in text, and additionally at least one row with title=”B” that contains the string “bbb” in text.

This is what I tried:

mysql> SELECT (SELECT text FROM table WHERE title = 'A') as aText, (SELECT text FROM table WHERE title = 'B') as bText;

I had planned on parsing the values of aText and bText for the strings “aaa” and “bbb” respectfully with PHP. However, I have two issues:

1) Major issue: Due to title not being unique, the subqueries may return multiple rows. MySQL breaks on that happening with this error:

ERROR 1242 (21000): Subquery returns more than 1 row

2) Minor issue: The reason that I am parsing in PHP is to avoid using MySQL’s LIKE operator. Would I be better off doing the parsing of the string right there in MySQL as such:

mysql> SELECT (SELECT text FROM table WHERE title = 'A' AND text LIKE '%aaa%') as aText, (SELECT text FROM table WHERE title = 'B' AND text LIKE '%bbb%') as bText;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:27:29+00:00Added an answer on May 28, 2026 at 3:27 am

    It is probably not good form to answer one’s own question, but for the sake of the fine archives at the expense of my karma:

    The query that I employed is:

    SELECT count(*) FROM (
        SELECT DISTINCT title FROM (
            SELECT title, text FROM table WHERE title IN ('A', 'B')
        )
        AS filtered
        WHERE (title='A' AND text LIKE '%aaa%')
           OR (title='B' AND text LIKE '%bbb%')
    )
    AS allResults;
    

    The innermost query gets everything with a good title, the next query gets distinct titles for the real results. This setup ensures that the LIKE clause will not run on the entire table. I then wrap all that in a count, and if if equals 2 then I know that each condition was met.

    Thank you everyone who contributed, I gained quite some insight from your answers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with 3 fields: ID (not unique, not primary key) timestamp
I have title (varchar), description (text), keywords (varchar) fields in my mysql table. I
I have a table with a collection of orders. The fields are: customerName (text)
i have database table with few text fields which i use to filter data
I have a simple data table which contains dynamic form text fields. Each field
We have table with several full text indexed fields. Say, it is firstName, surName
I have a table with each cell containing a label and a text field.
In a SQL server database, I have a table which contains a TEXT field
In one of the ms-access table I work with we have a text field
I have table with some fields that the value will be 1 0. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.