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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:10:55+00:00 2026-06-07T12:10:55+00:00

i have a datagridview row, containing three string values. These values should be looked

  • 0

i have a datagridview row, containing three string values. These values should be looked up into the Products table for finding the corresponding product ids. Then these will be inserted onto the Relations Table. I am looking for the best query to achieve this.

Here is my Products Table
+------------+--------------+
| Product_ID | Product_Name |
+------------+--------------+
|          1 | Foo          |
|          2 | Bar          |
|          3 | Baz          |
|          4 | NewProduct   |
+------------+--------------+

and the Relations Table i am trying to insert into

+------------+----------------+-----------------+
| Product_Id | RelatedProd_Id | RelatedProd_Id2 |
+------------+----------------+-----------------+
|          1 | 2              | null            |
|          2 | 3              | 1               |
|          3 | null           | null            |
+------------+----------------+-----------------+

The below one is not a table, it is a sample datagridview Row..

+------------+--------------+---------------+
|  ProdName  | RelProd_Name | RelProd_Name2 |
+------------+--------------+---------------+
| NewProduct | Foo          | Bar           |
+------------+--------------+---------------+

I am trying to find the ids from this row and insert it into the Relations table.

I tried a dumb query..but i dont’ know the correct way of doing it..something like,

INSERT INTO PROD_RELATIONS (Product_id,RelatedProd_Id,RelatedProd_Id2)
VALUES
(SELECT Product_Id FROM Products WHERE Product_Name = 'NewProduct'),
(SELECT Product_Id FROM Products WHERE Product_Name = 'Foo'),
(SELECT Product_Id FROM Products WHERE Product_Name = 'Bar')

Can somebody guide me on this?

  • 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-07T12:10:58+00:00Added an answer on June 7, 2026 at 12:10 pm

    With your current table structure a query like this would work:

    INSERT INTO Prod_Relations (Product_ID, RelatedProd_ID1, RelatedProd_ID2)
    SELECT  t1.Product_ID, t2.PRoduct_ID, t3.Product_ID
    FROM    Products t1, Products t2, Products t3
    WHERE   T1.Product_Name = 'NewProduct'
    AND     t2.Product_Name = 'Foo'
    AND     t3.Product_Name = 'bar'
    

    However, I’d recommend changing your relations table to a more simple layout with multiple rows per product:

    Product_ID  |   RelatedProd_ID
    ------------+-----------------
        4       |       1
        4       |       2   
    

    This means that you don’t have to add more columns if a product has more than 2 relations. In which case your insert statement would be:

    INSERT INTO Prod_Relations (Product_ID, RelatedProd_ID)
    SELECT  t1.Product_ID, t2.PRoduct_ID
    FROM    Products t1, Products t2
    WHERE   T1.Product_Name = 'NewProduct'
    AND     t2.Product_Name IN ('Foo', 'Bar')
    

    You can always query your product relations table to get it back to a 2 column format if necessary

    SELECT  t1.Product_ID, 
            MIN(t1.RelatedProd_ID) AS [RelatedProd_ID1],
            MIN(t2.RelatedProd_ID) AS [RelatedProd_ID2]
    FROM    Prod_Relations t1
            LEFT JOIN Prod_Relations t2
                ON t2.Product_ID = t1.Product_ID
                AND t2.RelatedProd_ID > t1.RelatedProd_ID
    GROUP BY t1.Product_ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView that I would like all values on a specific row
I have a DataGridView bound to a BindingSource. Each row has three fields: Name,
i have one datagridview.in my datagridview is having four row and three columns.after enter
I have a sortable DatagridView with a summary last row containing sum of some
I have a datagridview where the default values are provided for every new row
I have a virtual datagridview that I want to set varying row heights for.
I have a winforms datagridview that seems to always have at least one row
Possible Duplicate: how to delete row from datagridview with a delete button? I have
I have a dynamically created DataGridView that has a valid DataSource with one row
I have a DataGrid containing a small table. Until now, I had a double

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.