douxi0098 2014-03-18 21:02
浏览 47

在MYSQL中查询WHERE

I have two MySQL tables. One is called members and has a column called users. The other is called sex and has two columns: user_s and sex. I am trying to create an array comprised of all users from the members table who are 1) in the sex table and 2) have "men" selected in the sex column.

When I print_r this array nothing is displayed even though I can see that there are users who met these criteria. Any thoughts?

<?php 
 // test.php
include_once("header.php");
$iaminterestedin = "men"; 
$result = queryMysql("SELECT `user` FROM `members`
                WHERE `user` IN(SELECT `user_s` FROM `sex` WHERE sex='$iaminterestedin')"); 

$combination = array();
while(($row = mysql_fetch_assoc($result)))
{    
$combination = $row['user'];
}
print_r($combination);
?>
  • 写回答

3条回答 默认 最新

  • dpy33121 2014-03-18 21:20
    关注

    You should add values to combination using $combination[] = $row['user'] or you will always overwrite value on each loop iteration

    You also ought to read something about JOIN tables, this will help you a lot with queries like that. For example you will use:

    SELECT user FROM members INNER JOIN sex ON sex.user_s = members.user
    WHERE sex '$iamintererstedin'
    

    If you add a foreign key (http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html) from sex to users this will improve query performance when it gets bigger and time becomes a problem

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口