[英]How can i get the result from DB if one column have several keywrods?
Well, I've a table called keywords
and there are 2 columns. 好吧,我有一个名为keywords
的表,共有2列。
1) kid 1)小子
2) keywords (keyword value) 2)关键字(关键字值)
and I've another table called contact_details
where there a column called keyword
. 我还有一个名为contact_details
表,其中有一个名为keyword
的列。 In this keyword
column I'm inserting many keyword
from keywords
table. 在此keyword
列中,我从keywords
表中插入了许多keyword
。 So 2 tables is look like this... 所以2张桌子看起来像这样...
Keywords table: 关键字表:
kid keyword
1 php
1 mysql
1 html
1 css
1 css3
1 wp
1 photoshop
1 3d
contact_details table: 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
Now I've a search box which is searching people (name From contact_details table) by using keyword value. 现在,我有一个搜索框,该搜索框通过使用关键字值搜索人员(来自contact_details表的名称) 。 In search box, Search value could be few keywords. 在搜索框中,“搜索值”可能是几个关键字。 I mean it's could be php, mysql, html
or could be php, 3d, photoshop
. 我的意思是它可能是php, mysql, html
或可能是php, 3d, photoshop
。
So My question is : how can I write a Sql query to get the result ? 所以我的问题是:如何编写Sql查询以获得结果? I want to get all name which match the search keyword/s
from contact_details
table ? 我想从contact_details
表中获取与search keyword/s
匹配的所有名称吗?
Is there any field need to add in keywords
table ? keywords
表中是否需要添加任何字段? Can not get an IDEA :( 无法获得IDEA :(
Select name from contact_details where keyword like '%<search keywords>%'
Like search keyword key php then you need pass php in query and will get list of all name which having keywords as php' 像搜索关键字关键字php一样,那么您需要在查询中传递php,并将获得所有关键字的名称列表,其中关键字为php'
Select name from contact_details where keyword like '%php%'
Proper approach, Make kid as primary key in Keyword table 正确的方法,让孩子成为关键字表中的主键
Keywords table:
kid keyword
1 php
2 mysql
3 html
Remove keywords column from contact_details table. 从contact_details表中删除关键字列。
contact_details table:
cid name phone
1 alex 123
2 alex1 124
3 alex2 125
Make one more table which having many to many relationship and you need insert the relationship here so that no need touch again keywords and contact_details table again. 再创建一个具有多对多关系的表,您需要在此处插入关系,这样就无需再次触摸关键字和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 query (Not tested you can also use alias) SQL查询(未经测试也可以使用别名)
select name from contact_details join keyword_contact_mapping on kid_fk =(select kid from Keywords where keyword='php')
Assuming you have the ID when the name is selected, you can use: 假设选择名称时您具有ID,则可以使用:
SELECT Keyword from Keywords_Table
WHERE ID = <ID>
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.