简体   繁体   English

Mysql选择多个查询

[英]Mysql select multiple query

I have this table: 我有这张桌子:

------------------------------
|ID | name   | employee_code |
------------------------------
|24 | Robert |    20234      |
------------------------------

AND

-------------------------------------
|ID |   job_code   |     team       |
-------------------------------------
|24 | 241124 | Robert, Eduard, Etc. |
-------------------------------------

I want to search in second table by employee code and i try something like this: 我想通过员工代码在第二个表中搜索,我尝试这样的事情:

$sql=mysql_query("SELECT * FROM works WHERE (SELECT name FROM employee WHERE employee_code LIKE '%".$_GET['employee_code']."%' AS searchname) team Like %searchname% ");

Result: 结果:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Try this query - 试试这个查询 -

$employee_code = mysql_real_escape_string($_GET['employee_code']);

$sql=mysql_query("SELECT w.* 
                  FROM employee e
                  JOIN works w 
                  ON w.team LIKE CONCAT('%', e.name ,'%')
                  WHERE employee_code LIKE '%$employee_code%'");

see this SQLFiddle example - http://sqlfiddle.com/#!2/8f8b7/1 看到这个SQLFiddle示例 - http://sqlfiddle.com/#!2/8f8b7/1

You should be looking at a join . 你应该看一个加入。

select * from table1 inner join table2 using (`ID`) where job_code = ....

Then you have 1 row with both tables joined together 然后你有1行,两个表连接在一起

also your using mysql_* functions, These are no longer maintained please update to mysqli_* or PDO. 还有你使用mysql_ *函数,这些不再维护请更新到mysqli_ *或PDO。

Also you need to escape your queries, There an SQL injection attack waiting to happen in that code 此外,您需要转义您的查询,在该代码中等待发生SQL注入攻击

This would probably tell you exactly what was wrong. 这可能会告诉你究竟出了什么问题。

$sql=mysql_query("SELECT * FROM works WHERE (SELECT name FROM employee WHERE employee_code LIKE '%".$_GET['employee_code']."%' AS searchname) team Like %searchname% ");
if (!$sql)
    echo mysql_error();

You should never just assume that your query has worked and then carry on to use the resource in another command without checking that it did in fact work. 你永远不应该假设你的查询有效,然后继续在另一个命令中使用该资源,而不检查它确实工作。

Another thing you should not do is just put user input directly into SQL queries without any form of escaping as it will enable anyone to take complete control of your database. 您不应该做的另一件事是将用户输入直接放入SQL查询中而不进行任何形式的转义,因为它将使任何人都可以完全控制您的数据库。

SELECT * FROM works WHERE (SELECT name FROM employee WHERE employee_code LIKE '%".mysql_real_escape_string($_GET['employee_code'])."%' AS searchname) team Like %searchname% "

Your SQL query is wrong Try like this 您的SQL查询错误尝试这样

 SELECT * FROM works WHERE works.ID=employee.ID AND 
   employee.employee_code=".$_GET['employee_code']."
SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.employee_code = t2.job_code

or 要么

SELECT t1.id, t1.name, t2.team FROM table1 t1 INNER JOIN table2 t2 ON t1.employee_code = t2.job_code

for cleaner result 为了更清洁的结果

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

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