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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:33:56+00:00 2026-06-11T16:33:56+00:00

I have a MySQL database with a few tables. They look something like this

  • 0

I have a MySQL database with a few tables. They look something like this –

The food table:

+----------+------------+--------------+
| username | date       | food         |
+----------+------------+--------------+
| test123  | 2012-09-16 | rice         |
| test123  | 2012-09-16 | pizza        |
| test123  | 2012-09-16 | french fries |
| test123  | 2012-09-16 | burger       |
+----------+------------+--------------+

The main table:

+----------+------------+----------------+---------------+-------------+-------------+
| username | date       | water_quantity | water_chilled | smoked_what | smoke_count |
+----------+------------+----------------+---------------+-------------+-------------+
| test123  | 2012-09-16 |              1 | no            | cigarettes  |          20 |
+----------+------------+----------------+---------------+-------------+-------------+

When I use the query SELECT * FROM main,food WHERE main.date=food.date;, I get four rows as a result. How would it be possible that I get the results in a single row? Ultimately, when I encode the results into JSON, I want it to look something like this –

[
   {
      "username":"test123",
      "date":"2012-09-16",
      "water_quantity":"1",
      "water_chilled":"no",
      "smoked_what":"cigarettes",
      "smoke_count":"20",
       {
          "food":"rice",
          "food":"pizza",
          "food":"french fries",
          "food":"burger",                  
       },
   }
]

or something similar to. I am a newbie to MySQL and databases in general and also to JSON.. Thanks in advance for the help.

  • 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-11T16:33:58+00:00Added an answer on June 11, 2026 at 4:33 pm
    select m.*, GROUP_CONCAT(food SEPARATOR ',') AS food FROM main m INNER JOIN food f ON f.username = m.username and f.date = m.date;
    

    Of course you can change what fields you select to control the output but that will solve your duplication issue.

    As for the nested list of foods within the result set, you can use GROUP_CONCAT

    SEE: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

    I will see if I can recreate for demo


    DEMO:

    mysql> create table main (id INT NOT NULL AUTO_INCREMENT, username varchar(12) NOT NULL, date DATETIME, water_quality INT, water_chilled CHAR(3), smoked_what varchar(32), smoke_count INT, primary key (id));
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> create table food (id INT NOT NULL AUTO_INCREMENT, username varchar(12) NOT NULL, date DATETIME, food varchar(32), primary key (id));
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> insert into food VALUES (1,'test123','2012-09-16','rice'),(2,'test123','2012-09-16','pizza'),(3,'test123','2012-09-16','french fries'),(4,'test123','2012-09-16','burger');Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    mysql> insert into main VALUES (1, 'test123', '2012-09-16', 1, 'no', 'cigarettes', 20);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select m.*, GROUP_CONCAT(food SEPARATOR ',') AS food FROM main m INNER JOIN food f ON f.username = m.username and f.date = m.date;
    +----+----------+---------------------+---------------+---------------+-------------+-------------+----------------------------------+
    | id | username | date                | water_quality | water_chilled | smoked_what | smoke_count | food |
    +----+----------+---------------------+---------------+---------------+-------------+-------------+----------------------------------+
    |  1 | test123  | 2012-09-16 00:00:00 |             1 | no            | cigarettes  |          20 | rice,pizza,french fries,burger   |
    +----+----------+---------------------+---------------+---------------+-------------+-------------+----------------------------------+
    1 row in set (0.00 sec)
    
    mysql> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a few tables in a MySQL database similar to this setup: major
I have a legacy mysql database and there's this table which has a few
I have a MySQL database with a few (five to be precise) huge tables.
I have mysql database like this id | code 1 | bok-1 2 |
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a MySQL database that has a few very simple tables. I would
I have a MySQl database with a few tables, all UTF-8 and MyISAM storage.
I have a database with a few tables and columns. I would like to
I have a MySQL 5.0 database with a few tables containing over 50M rows.

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.