Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在一行的三列中查找userid_Php_Mysql - Fatal编程技术网

Php 在一行的三列中查找userid

Php 在一行的三列中查找userid,php,mysql,Php,Mysql,我需要检查10行中的3列,按降序匹配userid,然后找到一个userid并显示结果 假设这是我的桌子 表名提要 id buy_id sell_id sold_id 30 2 5 3 29 6 4 9 28 44 21 36 27 26 2 15 26 4 2 9 25 8 7 2 24 11 2

我需要检查10行中的3列,按降序匹配userid,然后找到一个userid并显示结果

假设这是我的桌子

表名提要

id   buy_id sell_id sold_id
30     2      5       3
29     6      4       9  
28     44     21      36
27     26     2       15
26     4      2       9
25     8      7       2
24     11     2       3
23     2      1       9  
例如:我们需要找到用户ID 2,并将限制设置为10输出

我只有一列,所以我不知道如何匹配一行中的3列

我想要10个输出

if userid = buy_id then echo "buyer";
if userid = sell_id then echo "seller";
if userid = sold_id then echo "third party";
当我们在每行的列中匹配userid 2时,输出应为:

1.buyer
2.seller
3.seller
4.third party
5.seller
6.buyer
由于userid2只有6个活动,所以只有6个输出,但我需要最大10个,最小0个

我还没有尝试过任何代码,因为我不知道如何处理这个问题

谢谢,希望我能说清楚:|

试试这个:

select id,
       case when buy_id = $userid then "buyer"
            when sell_id = $userid then "seller"
            else "third party"
       end who
from feeds
where $userid in (buy_id, sell_id, sold_id)
limit 10
SELECT *
FROM (
  (SELECT buy_id userid, id, 'buyer' type
   FROM feeds)
  UNION
  (SELECT sell_id userid, id, 'seller' type
   FROM feeds)
  UNION
  (SELECT sold_id userid, id, 'third party' type
   FROM feeds)
) feeds_data
WHERE feeds_data.userid = $CHOOSE_USERID
LIMIT 10
当您为
$choose\u USERID
选择
2
时,应返回如下表:

| userid | id |    type     |
|--------|----|-------------|
| 2      | 30 | buyer       |
| 2      | 27 | seller      |
| 2      | 26 | seller      |
| 2      | 25 | third party |
| 2      | 24 | seller      |
| 2      | 23 | buyer       |

您可以通过更改
$CHOOSE\u USERID
的值来自定义查询,还可以通过使用
id
字段或使用
feeds\u data
表别名的
type
字段进一步深入查询。

请添加预期结果。(列应该是什么?)?最终结果中需要哪些数据?身份证件用户ID?用户类型?是的,类似Barmar的东西贴在下面Hanks@Barmar我会尝试一下Tank you@Aiias我现在会尝试这让我更接近我想要的现在我可以添加我的自我感谢@Aiiaswell如果你还在这里,你可以帮我告诉我需要回应什么?当我使用id时,它给出1,2,3,4,5,6,7,8,9,10,当我使用类型时,它给出10次buyer@deerox-我不明白你最后的评论。你能澄清一下吗?