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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:00:46+00:00 2026-06-03T07:00:46+00:00

I have one table which stores tags for files. The tags can be of

  • 0

I have one table which stores tags for files. The tags can be of different types. There are many files, each with tags stored for them of different types.

columns: file, tagtype, tags

So let’s say I have two rows for one file:

cyclist.jpg | date | 04-05-2012
cyclist.jpg | face | true

I would like to select into one row, all tags for a file, such that I have a mysql result:

cyclist.jpg 04-05-2012 true

At present I carry out two queries for the above, one for each tagtype. I want to know if I can do some kind of join, with the same table, on the same columns (tags), just based on one shared column (file).

  • 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-03T07:00:48+00:00Added an answer on June 3, 2026 at 7:00 am

    If you want to join automatically two rows that means that probably you can just create one row without spliting it:

    file         | date       | face 
    cyclist.jpg  | 04-05-2012 | true
    

    Otherwise if you need to keep separate rows, just select all rows in one query:

    $res = mysql_query("select * from tags where file='cyclist.jpg'");
    

    and then iterate them to fulfil ‘merged’ record:

    $res = mysql_query("select * from tags_table where file='cyclist.jpg'");
    
    $mergedRow = array();
    
    while ($row = mysql_fetch_assoc($res)) {
        foreach ($row as $rowKey => $rowValue) {
            if (!array_key_exists($rowKey, $mergedRow)) {
                $mergedRow[$rowKey] = $rowValue;
            }
        }
    }
    

    Edit:

    As promised here’s a sample of joining two rows into one with single query, however I would still recommend $mergedRow approach in PHP to merging them 🙂

    CREATE TABLE `files_table` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `tag_type` enum('PARENT','CHILD') NOT NULL DEFAULT 'PARENT',
      `date` date DEFAULT NULL,
      `file` varchar(255) NOT NULL DEFAULT '',
      `face` smallint(4) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 
    

    Then you can ‘pair’ PARENT-CHILDREN sets with:

    SELECT t1.file, t1.date, t2.face
        FROM files_table AS t1 JOIN files_table AS t2 ON (t1.file = t2.file)
        WHERE t1.tag_type = 'PARENT' AND t2.tag_type = 'CHILD'
        GROUP BY file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table which stores products. Each product can have multiple colours.
I have a stored proc, SP1, which executes and does select on one table.
I have a table which has several one to many relationships with other tables.
I have a database table which stores membership renewals each year. When insert the
I have an invoices table which stores a single record for each invoice, with
I have a table, and there is no column which stores a field of
I have a table which stores the details of users. I can hardly have
I have one table TABLE_SUBJECT which contain a number of subjects. I need to
I have one database table which contains 8 columns. One of the columns is
I have one table, t1, which has fileds called userid, week and year fields.

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.