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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:15:06+00:00 2026-06-07T17:15:06+00:00

I have two tables that looks like this: Table: items id | itemId —|——

  • 0

I have two tables that looks like this:

Table: items

id | itemId
---|------
0  | 1
1  | 2
2  | 3

Table: item_specs

id | itemId | key          | values
---|--------|---------------
0  | 1      | itemreceived | 2012-06-01
1  | 1      | modelyear    | 1992
2  | 1      | model        | 2
3  | 2      | itemreceived | 2012-06-05
4  | 2      | modelyear    | 2003
5  | 2      | model        | 1
6  | 3      | itemreceived | 2012-07-05
7  | 3      | modelyear    | 2000
8  | 3      | model        | 3

My current query looks like this:

SELECT items.*, item_specs.* FROM item_specs
INNER JOIN item_specs ON items.itemId = item_specs.itemId
WHERE itemId IN(1,2,3)

How can I order the result by a key value, for example: model?

The result I’m looking for is something like this: (if I order by model)

id | itemId | key          | values
---|--------|---------------
3  | 2      | itemreceived | 2012-06-05
4  | 2      | modelyear    | 2003
5  | 2      | model        | 1
0  | 1      | itemreceived | 2012-06-01
1  | 1      | modelyear    | 1992
2  | 1      | model        | 2
6  | 3      | itemreceived | 2012-07-05
7  | 3      | modelyear    | 2000
8  | 3      | model        | 3

The content that is returned is ordered by the value that is that has the key model

  • 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-07T17:15:09+00:00Added an answer on June 7, 2026 at 5:15 pm

    You need the model number for every row. You can do that with a join:

    SELECT items.*, item_specs.* 
    FROM item_specs
    INNER JOIN item_specs ON items.itemId = item_specs.itemId
    INNER JOIN item_specs aux ON (aux.key = 'model' and aux.itemID = item_specs.itemId)
    WHERE item_specs.itemId IN(1,2,3)
    ORDER BY aux.values/*this is the model*/, item_specs.id;
    

    or with a subselect:

    SELECT items.*, 
           item_specs.*, 
           (select aux.values 
            from item_specs aux 
            where aux.key = 'model' and aux.itemID = item_specs.itemId
            ) as model
    FROM item_specs
    INNER JOIN item_specs ON items.itemId = item_specs.itemId
    WHERE item_specs.itemId IN(1,2,3)
    ORDER BY model, item_specs.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with two tables: Users , Items Users looks like this:
Let's say I have two tables that look like this: TH TH TH TH
I have two tables that look like this: Supplier SN SNAME Stat City +----------+-------+----+--------+
I have two tables like this create table A_DUMMY ( TRADE_ID VARCHAR2(16) TRADE_DATA VARCHAR2(500)
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
I have a table that looks like this: Id | Name | Parent ---+-------------------------+-------
I have two tables like this:- TABLE A ITEM LOCATION CODE A WHS QW123
I have two tables in my database that look like that: Customer: C_ID city
I have two tables that should be joined together by a foreign key relationship,
I have two tables that are related via a mapping table: keywords titles I

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.