如何使用PHP中数组中另一个键的值获取键搜索的所有值?(How to get all the values of a key searching with the value of another k

编程入门 行业动态 更新时间:2024-10-27 20:31:38
如何使用PHP中数组中另一个键的值获取键搜索的所有值?(How to get all the values of a key searching with the value of another key in an array in PHP?)

我有一张桌子上有我所拥有的汽车类型:

Table cars idCar | name -------+------- 1 | car1 2 | car2 3 | car3 4 | car4

另一张桌子上有购买每辆车的顾客:

Table sales idCar | idCustomer -------+------------ 2 | cust1 2 | cust5 3 | cust5 3 | cust2

并且我将所有销售表的值存储在使用PHP的数组中。 它看起来像这样:

array(4) { [0]=> array(2) { ["idCar"]=> string(1) "2" ["idCustomer"]=> string(5) "cust1" } [1]=> array(2) { ["idCar"]=> string(1) "2" ["idCustomer"]=> string(5) "cust5" } .... //Here the rest of values }

我想要做的是获得特定客户购买的汽车(或汽车)的所有价值,例如cust5 。

因此,如果我在销售数组中搜索已购买客户cust5的汽车,它应该返回给我, idCar的值为2和3 。 之后我将获得idCar的值和MySQL name 。

我试过使用以下sql语句:

"SELECT * FROM cars WHERE FIND_IN_SET(idCar, $salesTable)";

其中$salesTable是包含完整销售表的数组,但当然它不起作用,因为我试图直接解析数组,而不是idCar的值。

我只想将与特定客户匹配的idCar的值传递给sql语句,在本例中为cust5 。

是否有一些方法只获得PHP的某些函数所需的idCar值,只给出cust5作为参数,所以我可以应用上面提到的sql语句?

提前致谢!

I have a table with the types of cars that I have on stock:

Table cars idCar | name -------+------- 1 | car1 2 | car2 3 | car3 4 | car4

And another table with the customers that have bought each car:

Table sales idCar | idCustomer -------+------------ 2 | cust1 2 | cust5 3 | cust5 3 | cust2

and I have all the values of sales table stored in an array with PHP. It looks like this:

array(4) { [0]=> array(2) { ["idCar"]=> string(1) "2" ["idCustomer"]=> string(5) "cust1" } [1]=> array(2) { ["idCar"]=> string(1) "2" ["idCustomer"]=> string(5) "cust5" } .... //Here the rest of values }

What I am trying to do is to get all the values of a car (or cars) that have been bought by a particular customer, for example, cust5.

So, if I search on the sales array for the cars that have bought the customer cust5 it should return to me, idCar with the values 2 and 3. After I will get the values of idCar and name with MySQL with that values.

I have tried using the following sql statement:

"SELECT * FROM cars WHERE FIND_IN_SET(idCar, $salesTable)";

where $salesTable is the array that contains the full sales table but of course it does not work because I am trying to parse there an array directly, instead of the values of idCar.

I would like only to pass to the sql statement the values of idCar that match with a particular customer, in this case, with cust5.

Is there some way to get only the values of idCar required with some function of PHP giving only cust5 as parameter so I would be able to apply the sql statement that I put above?

Thanks in advance!

最满意答案

使用INNER JOIN

SELECT c.* FROM `cars` c INNER JOIN sales s ON s.idCar = c.id AND s.idCustomer = 'cust5'

Use INNER JOIN

SELECT c.* FROM `cars` c INNER JOIN sales s ON s.idCar = c.id AND s.idCustomer = 'cust5'

更多推荐

本文发布于:2023-08-06 04:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   组中   中数   PHP   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!