douxiangbiao1899 2013-05-07 12:44
浏览 75
已采纳

mysql:IF'some columns value'是DISTINCT THEN的反转布尔值

It seems that I'm asking a simple question but I haven't been able to find an answer that works for me. I hope you can help!

I'm writing a php script that takes some form data from an html-file (name, artist, song) and put it into a table with the columns (name, artist, song, queue, newcomer). Now, the column newcomer is a boolean; and I want this boolean value to change from the default of false or 0, to true, or 1, if the name value of the incoming form data is unique.

This is my try:

$newcomer = mysql_query(
                'IF NOT EXISTS (SELECT DISTINCT name FROM tbl_queue) 
                 THEN UPDATE tbl_queue newcomer=1'
            );

but obviously, it doesn't work...

Thanks in advance!!


Hi and thanks for the answer! HOwever it still doesn't work for me =( THis is the code from the php:

$newcomer = mysql_query("UPDATE tbl_queue SET newcomer=1 WHERE name='" . $_POST[name] . "' AND NOT EXISTS (SELECT DISTINCT name FROM tbl_queue WHERE name='" . $_POST[name] . "')" );

  • 写回答

4条回答 默认 最新

  • douyi3767 2013-05-07 13:06
    关注

    I would be tempted to do your initial insert like this:-

    INSERT INTO tbl_queue (name, artist, song, queue, newcomer)
    SELECT DISTINCT a.name, a.artist, a.song, a.queue, CASE WHEN b.newcomer IS NULL THEN 0 ELSE 1 END as newcomer
    FROM (SELECT '$name' AS name, '$artist' AS artist, '$song' AS song, '$queue' AS queue) a
    LEFT OUTER JOIN tbl_queue b
    ON a.name = b.name
    

    Have a dummy select of the values you want to insert, and left join that against the existing table based on name, using a CASE statement to check if a matching row was found or not.

    EDIT - Below is some php showing the var assignments and escaping the values. I have used mysql_ functions here as you have in your original post, but really should be using mysqli_ functions to future proof things.

    <?php
    
    $name = mysql_real_escape_string($_POST['name']);
    $artist = mysql_real_escape_string($_POST['artist']);
    $song = mysql_real_escape_string($_POST['song']);
    $fcfs_queue = mysql_real_escape_string($_POST['fcfs_queue']);
    
    $sql = ("INSERT INTO tbl_queue (name, artist, song, fcfs_queue, newcomer)
    SELECT DISTINCT a.name, a.artist, a.song, a.fcfs_queue, CASE WHEN b.newcomer IS NULL  THEN 0 ELSE 1 END as newcomer
    FROM (SELECT '$name' AS name, '$artist' AS artist, '$song' AS song, '$fcfs_queue' AS  fcfs_queue) a
    LEFT OUTER JOIN tbl_queue b
    ON a.name = b.name");
    
    
    if (!mysql_query($sql))
    {
        die('Error: ' . mysql_error());
    }
    echo "success";
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题