简体   繁体   中英

PHP SQL Query - Select All Associated w/ Specific Value?

I could use some assistance with an sql query. I have a small 3 column table, id, ip, and birthday. id auto increments.

I'm trying to select all birthdays that are associated with a specific ip, but I'm not sure if the SQL statement I wrote is correct. If someone could check this for me it would be appreciated.

$query = "SELECT birthday, COUNT(ip) FROM $table Group By ip HAVING ( COUNT(ip) > 1) WHERE ip=$ip";

To get all the birthdays with a specific IP you'd simply query:

SELECT birthday FROM table WHERE ip="desired value here"

If you're trying to count that information, then you could use:

SELECT ip, count(*) as `Total` FROM table WHERE id="desired value here" GROUP BY ip
Select birthday from $table where ip='<ip goes here>'

Be sure to properly escape your inputs. PDO or MySQLi prepared statements are advisable.

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