카테고리 없음

[PHP] if 조건이있는 MySQL

행복을전해요 2021. 1. 23. 00:22

이 시도

$where_text = ""; // initially blank

// here $user variable contains the current username

if($user!='admin') // check $user is admin or not if not 
{
   $where_text = " WHERE username='$user' ";  // update $where_text
   }
   
   $query = "SELECT * FROM `transaction` ".$where_text; // apply in the query
   

이제 $query변수로 쿼리를 사용할 수 있습니다.

-------------------

코드에서이 논리를 수행하고 싶지만 ORwhere 절에서를 사용하여 확인할 수 있습니다 .

SELECT * FROM Transaction t
WHERE t.username = 'ali' 
OR EXISTS (SELECT category FROM users where username= 'ali' AND category ='A')
-------------------

이것은 작동합니다.

SELECT * FROM Transactions WHERE username = 'username' OR (SELECT 1 FROM users WHERE username = 'username' AND category = 'A')


출처
https://stackoverflow.com/questions/22009860