简体   繁体   English

如何在Mysql&PHP中进行搜索引擎查询?

[英]How to make search engine query in Mysql&PHP?

I want to make a search engine in an intranet. 我想在Intranet中创建一个搜索引擎。 Now i use this clause in PHP. 现在,我在PHP中使用此子句。

$k = explode(" ",$_GET[key]);
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like  '%$k[0]%' or '%$k[1]%'";

But it seems doesn't work. 但这似乎行不通。 Do you know where is wrong? 你知道哪里错了吗?

Thanks in advance. 提前致谢。

Edit: 编辑:

$sql = "select * from entreprise, site, salarie where entreprise.*, site.*, salarie.* like  '%$k[0]%' or '%$k[1]%'";

I have modified the query clause. 我已经修改了查询子句。 With this code, i think you can know what i want to do. 使用此代码,我想您可以知道我想做什么。

I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table. 我想找到与企业表的所有列中的内容和员工表的所有列中的内容匹配的任何内容。

This: 这个:

$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like  '%$k[0]%' or '%$k[1]%'";

is not valid SQL. 是无效的SQL。 It is hard to guess what you want to do, but I'm trying anyway: you want to find employees, and search them by name or by enterprise that employs them. 很难猜测您想做什么,但无论如何我都在尝试:您想找到员工,并按姓名或雇用他们的企业来搜索他们。 Is that the case? 是这样吗 Or do you want to search employess and/or enterprises? 或者您想搜索雇员和/或企业?

EDIT 编辑

I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table. 我想找到与企业表的所有列中的内容和员工表的所有列中的内容匹配的任何内容。

Ok, first of all you should realize that SQL is probably not the best tool for this job. 好的,首先,您应该认识到SQL可能不是这项工作的最佳工具。 See the other commenter - his suggestions about sphinx and friends are good. 查看其他评论者-他对狮身人面像和朋友的建议很好。 But still, if you really want to: 但是,如果您确实想要:

$sql = '
    SELECT e.id, e.name
    FROM   enterprise e
    -- first, look in column1
    WHERE  e.column1 LIKE '."'%".$k[0]."%'".'
    OR     e.column1 LIKE '."'%".$k[1]."%'".'
    ...etc for all entries in k...
    OR     e.column1 LIKE '."'%".$k[N]."%'".' 
    -- then, look in column2
    OR     e.column2 LIKE '."'%".$k[0]."%'".'
    OR     e.column2 LIKE '."'%".$k[1]."%'".'
    ...and so on and so forth for all entries in $k and all columns in enterprise...
    UNION ALL
    SELECT s.id, s.name
    FROM   salarie s
    WHERE  ...and the same for columns of salarie...
    ...
    UNION ALL
    ...any other tables you want to search...
';

As you can see, not something that makes you happy. 如您所见,没有什么能让您开心。

Another approach that might give you more joy is having some overnight job to scan all rows in the tables you're interested in, parse the texts you want to search into separate words, and store those in a keyword table, and storing the association between an object from the source database and the keyword in a separate table. 另一种可能给您带来更多乐趣的方法是做一些通宵的工作,以扫描您感兴趣的表中的所有行,将要搜索的文本解析为单独的词,然后将它们存储在关键字表中,并存储源数据库中的对象和关键字在单独的表中。 You can then search the keyword table and use the id's and table names you find for a collection of keywords to build the actual query to retrieve those rows. 然后,您可以搜索关键字表,并使用找到的ID和表名来查找关键字集合,以构建实际查询以检索这些行。 This is what I do, and it works great. 这就是我所做的,而且效果很好。 It works better because there is a relatively small amount of words that you will encounter, whereas the collection of objects is quite possible very large. 它的效果更好,因为遇到的单词数量相对较少,而对象的集合很有可能非常大。

It's hard to exactly see what you're trying to do, but you need, in your SQL query, to specify : 很难确切地知道您要做什么,但是您需要在SQL查询中指定:

  • on which tables you are working, with a from clause 在哪个表上使​​用from子句
  • on which fields the search has to be done, in the where clause. where子句中,必须在哪些字段上进行搜索。
  • how the data between employees and enterprises are related : 员工和企业之间的数据如何关联:
    • do you want to search for entreprises and their employees ? 您是否要搜索企业及其员工?
    • for employees and there enterprises ? 对于员工又有企业?
    • for all enterprises and the employees when the employee or the enterprise contains the words ? 对于所有企业和雇员,当雇员或企业中包含以下单词?


You could use something like this to search for entreprises that contain the word, and get their employees to : 您可以使用类似这样的方法来搜索包含该词的企业,然后将其员工吸引到:

select *
from entreprise
    inner join employee on employee.id_entreprise = entreprise.id
where entreprise.name like '%word%'
    or entreprise.description like '%word%';


Or, to search for employees that match the criteria and get their entreprise too : 或者,要搜索符合条件的员工并获得他们的企业:

select *
from employee
    inner join entreprise on entreprise.id = employee.id_entreprise
where employee.name like '%word%';


(just some ideas -- you'll have to build from there !) (只是一些想法-您必须从那里开始!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM