简体   繁体   中英

MYSQL retrieving a field value based on another

MYSQL I would like to retrieve a value of a field in columnX where a field value for columnY is specified. There maybe more than one row where this occurs but the first one will do. it would be excellent if you could provide the php. Thanks

Try this SQL:

SELECT `columnX` FROM `table` WHERE `columnY` = $value LIMIT 1

In PHP:

$query = $mysqli->query("SELECT `columnX` FROM `table` WHERE `columnY` = $value LIMIT 1");
$row = $query->fetch_assoc();
echo $row['columnX'];

IF you want to do it in sql you can use the IF condition like :

SELECT IF(X=0,Y,1) As cond FROM table

In PHP:

$query = $mysqli->query('SELECT IF(X=0,Y,1) As cond FROM table');
$row = $query->fetch_assoc();
echo $row['cond'];

thanks to "Daniel Li"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM