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 8117795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:17:26+00:00 2026-06-06T04:17:26+00:00

I’m designing a ticket system in PHP/MySQL. Each ticket represents one row in the

  • 0

I’m designing a ticket system in PHP/MySQL.
Each ticket represents one row in the ‘tickets’ table.
Users can add multiple ‘comments’ to each ticket.

When viewing a ticket I just do an SQL query on the ‘comments’ table to select all comments linked to the corresponsing ticketid

However, now I have to design a search page, which returns any matching tickets and immediately show (part of, e.g. last 5) their linked comments.

I have this working by performing the same SQL query I use in the single ticket view page, but repeating this for every matching row in the search query. So if you have 1500 matching rows, that means 1500 SQL queries on the ‘comments’ table, yes, not very efficient…

Now I was wondering: Is there a way to join the comments table to the tickets table, but have multiple rows from the comments table ‘squeezed’ into one column?

I was thinking about doing a CONCAT() on the comments rows, returning them as comma-seperated values, which I could then explode() again in php to get an array containing all my comments for each ticket. But is this the most effective way or is there something better?

EDIT: Keep in mind that tickets which don’t have any comments yet should also we returned by the query

  • 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-06-06T04:17:28+00:00Added an answer on June 6, 2026 at 4:17 am

    Zane Bien answered the CONCAT question, I won’t comment it because there is not much to add, but you also asked if it is the most effective way so I’ll go for this one.

    I really don’t like the CONCAT solution. It’s not the philosophy of SQL and you never know what users will write in the comment area.

    As far as SQL is concerned, the right way of doing it is:

    SELECT
        tickets.*,
        comments.*
    FROM tickets
    LEFT JOIN comments ON comments.ticket_id = tickets.id
    WHERE tickets.title LIKE '%whatever%';
    

    Then in PHP you simply iterate over the resultset and build the output. Note that the LEFT JOIN will rightfully return tickets without comments.

    If you feel a bit uneasy – but should not ! – having the ticket information on each line you can split the query in 2:

    SELECT
        tickets.*
    FROM tickets
    WHERE tickets.title LIKE '%whatever%';
    

    And

    SELECT
        comments.*
    FROM comments
    INNER JOIN tickets ON comments.ticket_id = tickets.id
    WHERE tickets.title LIKE '%whatever%';
    

    That implies some more work on the application side as you’ll have to link back the comments to the ticket.

    My advice: go for the all-in-one query.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns 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.