English 中文(简体)
1. 在Serialized Array条款中寻求价值
原标题:Search for a value in where clause from Serialized Array
  • 时间:2010-07-29 17:49:00
  •  标签:
  • php
  • sql

标题可能混淆不清,但我认为我怀疑清楚。

我要解释一下。 我在此问:

 $sql =  SELECT * FROM calendar WHERE day = " .$day. " AND month = " .$month. " AND year = " .$year. " AND realizada = 0 AND colaborador = "What to put here?!" ;

但是,实地“实验室”是一个序列式阵列。

一例:Iprint_r 我的阵列在进行空中勘测后的价值就是如此:

Array (0] => l3g iii 页: 1

我想在前几句中寻求“l3g”的魔ine,我如何能够这样做?

谢谢。

l3g

最佳回答

如果你需要从你的阵列中询问个别内容,则不储存序列号。 在每个儿童桌上储存每个单元,并将其与贵日历表的主要价值联系起来:

CREATE TABLE colaboradoras (
  calendar_id INT NOT NULL,
  colaborador VARCHAR(20) NOT NULL,
  FOREIGN KEY (calendar_id) REFERENCES calendar(calendar_id)
);

INSERT INTO colaboradoras VALUES
(1234,  l3gion ),
(1234,  Someone ),
(1234,  teste );

$sql = "SELECT * FROM calendar AS c JOIN colaboradoras AS o 
              ON c.calendar_id = o.calendar_id
        WHERE c.day = $day AND c.month = $month AND c.year = $year 
          AND c.realizada = 0 AND o.colaborador =  l3gion ";

这是正常的做法。

如果你必须储存序列式阵列,你可以检查HowvantFeed Uses MySQL,以指数化“schemaless”数据。 但这也涉及为指数编制新的表格。

如果你真的能够创建任何新的表格或索引,你可以尝试使用<条码>LIKE或<条码>,但这两种解决办法都将非常低效和容易发生错误。

SELECT ... WHERE ... AND colaborador REGEXP  [[:<:]]s:6:"l3gion"; 

你再说一遍。

问题回答

Colaborador = “What to put here?

该法典将处理序列化和原始数据以及阵列和便衣价值。

if (is_string($colaborador)) {
    // prevent errors from showing
    ob_start();
    $data = unserialize($colaborador);
    // $data is now false or output buffer is non-empty if $colaborador was
    // not serialized
    $data = ob_get_length() > 0 || false === $data ? $colaborador : $data;
    // lose the possible error
    ob_end_clean();
} else {
    $data = $colaborador;
}
// use the first value as colaborador if an array
$sql =  SELECT * FROM calendar WHERE day = " .$day. " AND month = " .$month. "
AND year = " .$year. " AND realizada = 0 AND colaborador =    .
(is_array($data) ? (empty($data) ?    : $data[0]) : $data) .    ;`

或者,如果你想找他们的话:

$sql =  SELECT * FROM calendar WHERE day = " .$day. " AND month = " .$month. "
AND year = " .$year. " AND realizada = 0 AND colaborador = IN (   .
(is_array($data) ? (empty($data) ?    : implode(" ,  ", $data[0])) : $data) .   ) ;`




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签