根据PHP中的列条件处理MySQL查询

时间:2015-07-16 12:32:55

标签: php mysql unicode

我有一个脚本,它将短信发送给所有成员。首先,当我发送短信时,我存储在MySQL数据库中,然后从不同的脚本中获取相同的内容并批量发送到我的短信网关提供商。

计划短信表包含以下列。

`id, to, senderid, message, unicode, status`

Unicode有3个值为1,2,3

1 = english
2 = unicode
3 = flash

然后我在PHP中运行这样的查询

 $sql = "SELECT `to`, `senderid`, `message` FROM `sch_sms` WHERE `status`='0' AND `unicode` = '1' LIMIT 1000

然后我创建for loop并使用cron发送短信。

目前我可能需要编写上述sql查询3次,并以不同的方式运行3次以根据unicode条件发送短信。

我想知道的是,是否可以对所有3个不同的unicode值进行单个查询,然后单个for loop然后处理我的php短信函数。

非常感谢任何建议。

2 个答案:

答案 0 :(得分:4)

使用IN查看代码是否在列中:

SELECT `to`, `senderid`, `message`, `unicode` 
FROM `sch_sms` 
WHERE `status`='0' 
AND `unicode` IN('1','2','3') 
LIMIT 1000

如果这些是该列中唯一的三个值,则可以完全放弃该条件:

SELECT `to`, `senderid`, `message`, `unicode`
FROM `sch_sms` 
WHERE `status`='0' 
LIMIT 1000

完成上述操作后,您可以设置一个while循环,您可以使用switch语句来确定消息的发送方式(伪代码) -

while($row = <appropriate database function here>) {

    switch ($row['unicode']) {
    case 1:
        // code for this send type
    break;
    case 2:
        // code for this send type        
    break;
    case 3:
        // code for this send type        
    break;
}

答案 1 :(得分:2)

正如我所说的那样

如果你必须“批量”发送它们到你的短信网关使用ORDER BY语句并检查当前记录是否具有与最后一个相同的unicode值;如果不是......那就是下一组。

(不确定你是否真的需要这个......)

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
    PDO::MYSQL_ATTR_DIRECT_QUERY => false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo);

$currentUnicode = null;
foreach( $pdo->query('SELECT unicode, message FROM soFoo WHERE status=0 ORDER BY unicode', PDO::FETCH_ASSOC) as $row ) {
    // check if this message is the first of a new unicode-group
    if ( $row['unicode']!==$currentUnicode ) { 
        switchCode($row['unicode']);
        // it's the new "current" group
        $currentUnicode=$row['unicode'];
    }

    sendSMS($row);
}

function switchCode($newCode) {
    echo "--- switching to new Code: $newCode ---\r\n";
}

function sendSMS($data) {
    echo "sending msg: {$data['message']}\r\n";
}

// boilerplate, creates temp table and inserts sample data....
function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo (
            id int auto_increment,
            message varchar(32),
            unicode int,
            status int,
            primary key(id)
        )
    ');
    $stmt = $pdo->prepare('INSERT INTO soFoo (message,unicode, status) VALUES (?,?,0)');
    $stmt->bindParam(1, $message);
    $stmt->bindParam(2, $unicode);

    foreach( range('a','k') as $c ) {
        foreach(range(1,3) as $unicode) {
            $message = sprintf('msg%d%s', $unicode, $c);
            $stmt->execute();
        }
    }
}

打印

--- switching to new Code: 1 ---
sending msg: msg1a
sending msg: msg1g
sending msg: msg1e
sending msg: msg1h
sending msg: msg1d
sending msg: msg1i
sending msg: msg1c
sending msg: msg1j
sending msg: msg1b
sending msg: msg1k
sending msg: msg1f
--- switching to new Code: 2 ---
sending msg: msg2i
sending msg: msg2j
sending msg: msg2h
sending msg: msg2g
sending msg: msg2k
sending msg: msg2f
sending msg: msg2c
sending msg: msg2a
sending msg: msg2e
sending msg: msg2d
sending msg: msg2b
--- switching to new Code: 3 ---
sending msg: msg3a
sending msg: msg3j
sending msg: msg3b
sending msg: msg3i
sending msg: msg3f
sending msg: msg3c
sending msg: msg3h
sending msg: msg3d
sending msg: msg3g
sending msg: msg3e
sending msg: msg3k

switchCode()将是让你的短信网关知道切换unicode的功能。

编辑:
在pseude-code中,您的工作流程可能类似于

if ( changed(unicode) or changed(message) or toolong($to+row['to']) ) {
    send current sms request to gateway
    reset unicode, message, to
}
else {
    concat $to $row['to']
}

这是非常不合适的,但您可能需要考虑不同的表格布局,例如

CREATE TABLE smsUsers (
    user_id int auto_increment,
    user_unicode int,
    user_phonenumber ...,
    ...
)

CREATE TABLE smsMessages (
    message_id int auto_increment,
    message varchar(32),
    ...
)

CREATE TEMPORARY TABLE smsMessageQueue (
    queue_id int auto_increment,
    message_id int,
    user_id int,
    queue_status int,
    ...
)

至少可以降低识别不同消息文本的成本,并可能节省数据库中的一些空间。