drux41001 2014-04-03 01:05
浏览 20
已采纳

mySQL错误的逻辑语句,不正确的下拉

Attached is a picture that pretty much describes my entire problem:

Direct link: http://i.imgur.com/IAqqaWZ.png

I have a drop down table. Normally, it shows four entries, all the ones I want via this statement:

$sql="SELECT users.first_name, users.last_name, users.user_id, payments.fk FROM users, payments WHERE landlord = '0'";

So everything works up until I change up the sql line a bit to this:

$sql="SELECT users.first_name, users.last_name, users.user_id, payments.fk FROM users, payments WHERE landlord = '0' AND payments.fk != 'users.user_id'";

Basically, what I'm trying to select, is everyone that has the landlord flag set to 0 (which works fine in the previous code above), AND out of those people with the landlord flag set to 0, I want to only pick those people who aren't in the payments table. So basically, what should happen, is the drop down should only show three people.. Instead, just by adding that AND statement to my code, it now shows 4 people (4 times each for some reason), including the one person who is in the payments table.

Does anyone have any idea how I can fix this? Sorry for the crappy explanation.

  • 写回答

1条回答 默认 最新

  • dssk35460 2014-04-03 01:40
    关注

    Try using a LEFT JOIN, and check if the payment field IS NULL

    SELECT users.first_name, users.last_name, users.user_id, payments.fk 
    FROM users
    LEFT JOIN payments 
    ON payments.fk = users.user_id
    WHERE landlord = 0 AND payments.fk IS NULL
    

    SQLFiddle example - http://sqlfiddle.com/#!2/46a88c/2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗