0

I have a table which holds events. Each event has a name, but events are grouped based on their name.

+---------------+
| NAME          |
+---------------+
| EVENT 1 CHILD |
| EVENT 1 ADULT |
+---------------+

Is a sample row. However, when I query for the events, I only want to show one of the event names.

So essentially I only want EVENT 1 ADULT to be displayed, as the child is linked to it via it's name.

I'm not sure how I would do this purly in MySQL, I know I could perform a query then strip out the events with PHP, but it seems silly to do this if I can do it with MySQL.

Any advice on how to tackle this problem?

Thanks!

Peon
  • 7,902
  • 7
  • 59
  • 100
dotty
  • 40,405
  • 66
  • 150
  • 195

2 Answers2

4

You can just read the date where there is only ADULT like this:

SELECT * FROM table WHERE NAME LIKE '%ADULT%';

Or, if it is always at the end:

SELECT * FROM table WHERE NAME LIKE '% ADULT';

But, if you need it case sensitive, try this:

SELECT * FROM table WHERE NAME LIKE BINARY '%ADULT%';
Peon
  • 7,902
  • 7
  • 59
  • 100
0

You should change your table structure to include a parent_id column. This will allow you to explicitly link child records to a parent without basing it on the name and then you can easily query for just parent records using:

SELECT name FROM events WHERE parent_id IS NULL;

EDIT:

If you can't change the data (per your comment) then you're stuck with something like this for finding parents:

SELECT name FROM events WHERE name LIKE '%ADULT%';

And for finding the child of a given adult:

SELECT name FROM events WHERE name LIKE CONCAT('%', TRIM(TRAILING 'ADULT' FROM '$parentName', '%');

Note that is a totally untested use of CONCAT with a LIKE so it's just a conjecture that it will work.

davidethell
  • 11,708
  • 6
  • 43
  • 63