简体   繁体   English

如何使用PHP和Mysql从数据库中获取数据来比较某些值

[英]How to fetch data from database comparing some value using PHP and Mysql

I have some doubt. 我有一些疑问。 Actually i need to fetch data as per column value by joining two table using PHP and MySQL .I am explaining my tables below. 实际上我需要通过使用PHP和MySQL连接两个表来按列值获取数据。我正在解释下面的表格。

db_specials: db_specials:

id        special                  type

1        Food and Beverage          1

2        Liquor                     2

db_basic: db_basic:

 id      special_id     name

 1         2            aaa

 2         1            Raj

 3         2            Ram

 4         2            Jay

 5         1            Nikesh

Here i need to fetch all data from the tables db_basic those are associated with Liquor from first table. 在这里,我需要从表格db_basic获取所有数据,这些数据与第一个表格中的Liquor相关联。 I am explaining my code below. 我在下面解释我的代码。

$res=mysqli_query($connect,"select b.id,b.special_id,b.name,s.type from db_basic as b inner join db_specials as s on b.special_id=s.id where b.special_id=2 order by b.id desc");

while($row=mysqli_fetch_array($res)){
   $data[]=$row;
}

I am getting also the proper data. 我也得到了正确的数据。 Here problem is db_specials also has the delete functionality on front end with insert. 这里的问题是db_specials还在前端有insert的删除功能。 Suppose user deleted the row which has id=2 in db_specials table and insert again,in this case the id will change 2 to 3. So the query also needs to change accordingly. 假设用户在db_specials表中删除了id=2的行并再次插入,在这种情况下, id将更改为2到3.因此查询也需要相应地更改。 Here i need to know what should be the best logic so that each time user will not change the query if any specials is deleted and inserted again. 在这里,我需要知道什么是最好的逻辑,以便每次用户不会更改查询,如果任何特殊项被删除并再次插入。 Please help me. 请帮我。

Instead of id column, you should rely on the type column of db_specials table, since it's not going to be auto incremented for each INSERT operation. 您应该依赖于db_specials表的type列,而不是id列,因为它不会为每个INSERT操作自动递增。 So your query should be like this: 所以你的查询应该是这样的:

select b.id,b.special_id,b.name,s.type 
from db_basic as b 
inner join db_specials as s 
on b.special_id=s.type 
where b.special_id=2 
order by b.id desc

Also, I recommend that you should change the special_id column name of db_basic table to type , like this: 另外,我建议你应该将db_basic表的special_id列名更改为type ,如下所示:

+------+------+------+
|  id  | type | name |
+------+------+------+
|      |      |      |

This way, it would be easier for you to construct the query, like this: 这样,构建查询会更容易,如下所示:

select b.id,b.type,b.name
from db_basic as b 
inner join db_specials as s 
on b.type=s.type 
where b.type=2 
order by b.id desc

if user deleted 2,Liquor,2 in db_specials and reinsert, you cannot control what the user input in the front end. 如果用户在db_specials中删除2,Liquor,2并重新插入,则无法控制用户在前端输入的内容。
User might be insert Food,2 用户可能会插入食物,2

Instead of join the type column of db_specials with db_basic , you should put a checking in the front end, when user click the delete button, prompt a message before delete 2,Liquor,2 from the table db_specials, if there is any special_id=2 in table db_basic the corresponding rows will deleted as well. 不要使用db_basic连接db_specials类型列, 应该在前端放置一个检查,当用户单击删除按钮时,在表db_specials中删除2,Liquor,2之前提示消息,如果有任何special_id = 2在表db_basic中 ,相应的行也将被删除。

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

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