Link to home
Start Free TrialLog in
Avatar of JCWEBHOST
JCWEBHOST

asked on

sql join two tables

hey guys i have two table and need to join in one table using a select statement.

table one : menu
id
parent_id
title

table two: products
id
menu_id
name

my problem is the indentity in the id for bought table, so if they are enterd twice a erorr will oucur.

how can i fix the problem?
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Use the table names as prefixes to distinguish the IDs:

SELECT Table1.ID, Table2.ID, etc...
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.Menu_ID
User generated image
CREATE TABLE [dbo].[TableMenu](
	[Id] [int] NOT NULL,
	[ParentId] [int] NULL,
	[Title] [nchar](20) NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[TableProducts](
	[Id] [int] NULL,
	[Menu_Id] [int] NULL,
	[Name] [nchar](20) NULL
) ON [PRIMARY]

GO

SELECT	* 
FROM	TABLEMENU

SELECT	* 
FROM	TABLEPRODUCTS

SELECT	TM.ID,TITLE		AS MAINMENU,
		NAME			AS SUBMENU 
FROM	TABLEMENU TM
LEFT 
JOIN	TABLEPRODUCTS TP 
ON		TM.ID = TP.MENU_ID

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial