根据下一个字段中的值选择或不选择字段


Select, or not select a field based on a value in the next field

从这样的表(这只是这里的):

p_add  | p_add_s | p_phone | p_phone_s | p_user_id //0 = show 1= Hide
Address| 0       | 12345   | 1         | 1
Addr   | 0       | 12345   | 1         | 2

我如何进行选择,如:

 select p_add (if p_add_s = 0), p_phone (if p_phone_s=0) from table where p_user_id=1
or
    select p_add, p_phone from table where p_user_id=1 (if p_add_s = 0) and (if p_phone_s = 0)

这些都是不正确的。我把它们包括在内只是为了让你有个想法。

实际上,只有当_s的值为0时,我才需要数据。

这可以使用sql来完成吗?还是必须使用php来实现?

我想您正在寻找mysql IF语法:

select if(p_add_s=0, p_add, 'Address Private'),  if(p_phone_s=0, p_phone, 'Phone Private') from myTable

如果满足条件,则返回中间部分,否则返回最后一个。

或者,根据您的第二个查询:

select p_add, p_phone from table where p_user_id=1 and p_add_s = 0 and p_phone_s = 0

请注意,这不会返回所有不满足where子句中条件的行,因此if语句可能工作得更好。

select p_add, p_phone from table where p_user_id=1 and p_add_s=0 and p_phone_s=0
SELECT p_add FROM yourtable
WHERE p_user_id = 1
AND p_add_s = 0
AND p_phone_s = 0

不要忘记使用PDO。