好吧,我有一个名为keywords
的表,有2列。
1)孩子
2)关键字(关键字值)
我还有一个名为contact_details
的表,其中有一个名为keyword
的列。在这个keyword
列中,我从keyword
表中插入了许多keywords
。所以2个表看起来像这样......
关键字表:
kid keyword
1 php
1 mysql
1 html
1 css
1 css3
1 wp
1 photoshop
1 3d
contact_details表:
cid name phone keyword
1 alex 123 php, mysql, hmtl
2 alex1 124 php, html, css3
3 alex2 125 wp, html, css
4 alex3 126 photoshop, 3d
5 alex4 127 html, 3d, php
6 alex5 128 mysql, wp, html
现在我有一个搜索框,它使用关键字值搜索(来自contact_details表的名称)。在搜索框中,搜索值可能很少。我的意思是它可能是php, mysql, html
或可能是php, 3d, photoshop
。
所以我的问题是:如何编写 Sql查询来获得结果?我想从search keyword/s
表中获取与contact_details
匹配的所有名称?
是否需要在keywords
表中添加任何字段?无法获得IDEA :(
答案 0 :(得分:2)
Select name from contact_details where keyword like '%<search keywords>%'
像搜索关键字关键字php那么你需要在查询中传递php,并获得所有名称的列表,其中包含关键字为php&#39;
Select name from contact_details where keyword like '%php%'
正确的做法, 将孩子作为关键字表格中的主键
Keywords table:
kid keyword
1 php
2 mysql
3 html
从contact_details表中删除关键字列。
contact_details table:
cid name phone
1 alex 123
2 alex1 124
3 alex2 125
再创建一个具有多对多关系的表,您需要在此处插入关系,以便不再需要再次触摸关键字和contact_details表。
keyword_contact_mapping
kcid kid_fk cid_fk
1 1 1
2 1 2
3 1 3
4 2 1
5 2 1
6 2 2
7 2 3
Sql查询(未经测试,您也可以使用别名)
select name from contact_details join keyword_contact_mapping on kid_fk =(select kid from Keywords where keyword='php')
答案 1 :(得分:0)
假设您在选择名称时拥有ID,则可以使用:
SELECT Keyword from Keywords_Table
WHERE ID = <ID>
答案 2 :(得分:0)
USE AdventureWorksDW2008R2
GO
IF OBJECT_ID('Keywords') IS NOT NULL
BEGIN
DROP TABLE Keywords
END
IF OBJECT_ID('Contact_Details') IS NOT NULL
BEGIN
DROP TABLE Contact_Details
END
IF OBJECT_ID('Keyword_ContactDetails') IS NOT NULL
BEGIN
DROP TABLE Keyword_ContactDetails
END
/* automate id's with identity. Or, do you want to specify the the keyword id manually?
*/
CREATE TABLE Keywords(
Keywords_ID INT IDENTITY(1,1) NOT NULL
,Keyword NVARCHAR(100)
CONSTRAINT PK_Keywords PRIMARY KEY
(
Keywords_ID
)
)
/* You must plan each column data type by careful consideration
I am using the phone example here to demonstrate different business requirements
the lenghts and datatype may need to change for localization
*/
CREATE TABLE Contact_Details
(
Contact_Details_ID INT IDENTITY(1,1) NOT NULL
,First_Name VARCHAR(100)
,Last_Name VARCHAR(100)
,Phone VARCHAR(10)
,Phone_EXT VARCHAR(3)
,Phone_International NVARCHAR(15)
CONSTRAINT PK_Contact_Details PRIMARY KEY
(
Contact_Details_ID
)
)
CREATE TABLE Keyword_ContactDetails
(
Keyword_ID INT
,Contact_Details_ID INT
,DateTime_Created DATETIME
CONSTRAINT PK_KeywordContact PRIMARY KEY
(
Keyword_ID
,Contact_Details_ID
)
/*Enforce referential integrity,
prevents adding keywords that don't exist
prevents deleting a keyword if it is referenced
*/
FOREIGN KEY (Keyword_ID) REFERENCES Keywords(Keywords_ID),
FOREIGN KEY (Contact_Details_ID) REFERENCES Contact_Details(Contact_Details_ID)
)
/* Populate keywords
*/
INSERT INTO Keywords(Keyword) VALUES ('PHP')
INSERT INTO Keywords(Keyword) VALUES ('MYSQL')
INSERT INTO Keywords(Keyword) VALUES ('HTML')
INSERT INTO Keywords(Keyword) VALUES ('CSS')
/* Add contact details
*/
INSERT INTO Contact_Details(
First_Name
,Last_Name
,Phone
,Phone_EXT
,Phone_International)
VALUES(
'Abe'
,'Lincoln'
,'2129996677'
,'123'
,'na')
/* Assign PHP Keyword to Abe Lincoln
*/
DECLARE @keywordID int = 0
,@contactDetails int = 0
set @keywordID = (select Keywords_ID from Keywords where Keyword = 'PHP')
set @contactDetails = (select Contact_Details_ID from Contact_Details where Last_Name = 'Lincoln')
INSERT INTO Keyword_ContactDetails(
Keyword_ID
,Contact_Details_ID
,DateTime_Created)
VALUES(
@keywordID
,@contactDetails
,CURRENT_TIMESTAMP)
SELECT * FROM Contact_Details C
JOIN Keyword_ContactDetails KC
ON KC.Contact_Details_ID = C.Contact_Details_ID
JOIN Keywords K
ON K.Keywords_ID = KC.Keyword_ID