Архив

Архив раздела ‘MySQL’

Чтобы, например, при поиске в таблице не учитывались html теги вместо LIKE стоит использовать REGEXP. Пример в двумя вариантами:

SELECT * from `tablename` WHERE ( `name` LIKE '%строка поиска%') OR ( `content` REGEXP '>[^<]*строка поиска');
Categories: MySQL Tags:
SELECT CONCAT('ALTER TABLE `', t.`TABLE_SCHEMA`, '`.`', t.`TABLE_NAME`, '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') as sqlcode
FROM `information_schema`.`TABLES` t
WHERE 1
AND t.`TABLE_SCHEMA` = 'db_name'
ORDER BY 1

Результатом будет группа sql-запросов, которую необходимо будет выполнить.

Categories: MySQL Tags:
16 января 2014 Нет комментариев

Проблема при запросе с WHERE field>"143.6" или field<"143.6" т.е. больше или меньше указанного значения (но не равно) получаем также строку где значение поля равно 143.6.
Используемый тип данных: FLOAT
Решением может быть следующее: прибавить или вычесть пренебрежительно малую величину после чего сравнивать.

if ($pos=='>') {
	$item[$so]=$item[$so]+0.001;
}
if ($pos=='<') {
	$item[$so]=$item[$so]-0.001;
}

После чего запрос будет выглядеть: field>"143.601" или field<"143.599".
Читать далее...

Categories: MySQL Tags:
10 апреля 2013 Нет комментариев

mysql --user=dblogin --password=dbpass dbname < dbdump.sql

Categories: MySQL, Unix Tags:
10 апреля 2013 Нет комментариев

mysqladmin — создать новую базу данных
mysqladmin -u root -p create db_name

Categories: MySQL, Unix Tags:
10 марта 2013 Нет комментариев

В примере нужно собрать массив sql-запросов из файла дампа sql.

<?php
function remove_comments(&$output) {
	$lines = explode("\n", $output);
	$output = "";
	$linecount = count($lines);
	$in_comment = false;
	for($i = 0; $i < $linecount; $i++) {
		if( preg_match("/^\/\*/", preg_quote($lines[$i])) ) {
			$in_comment = true;
		}
		if( !$in_comment ) {
			$output .= $lines[$i] . "\n";
		}
		if( preg_match("/\*\/$/", preg_quote($lines[$i])) ) {
			$in_comment = false;
		}
	}
	unset($lines);
	return $output;
}
function remove_remarks($sql) {
	$lines = explode("\n", $sql);
	$sql = "";
	$linecount = count($lines);
	$output = "";
	for ($i = 0; $i < $linecount; $i++) {
		if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0)) {
			if ($lines[$i][0] != "#") {
				$output .= $lines[$i] . "\n";
			}
			else {
				$output .= "\n";
			}
			$lines[$i] = "";
		}
	}
	return $output;
}
function split_sql_file($sql, $delimiter) {
	$tokens = explode($delimiter, $sql);
	$sql = "";
	$output = array();
	$matches = array();
	$token_count = count($tokens);
	for ($i = 0; $i < $token_count; $i++) {
		if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0))) {
			$total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
			$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
			$unescaped_quotes = $total_quotes - $escaped_quotes;
			if (($unescaped_quotes % 2) == 0) {
				$output[] = $tokens[$i];
				$tokens[$i] = "";
			}
			else {
				$temp = $tokens[$i] . $delimiter;
				$tokens[$i] = "";
				$complete_stmt = false;
				for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++) {
					$total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
					$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
					$unescaped_quotes = $total_quotes - $escaped_quotes;
					if (($unescaped_quotes % 2) == 1) {
						$output[] = $temp . $tokens[$j];
						$tokens[$j] = "";
						$temp = "";
						$complete_stmt = true;
						$i = $j;
					}
					else {
						$temp .= $tokens[$j] . $delimiter;
						$tokens[$j] = "";
					}
				}
			}
		}
	}
	return $output;
}
?>

Использование, учитывая что $ungzdata — текст из файла sql-дампа.

$SQL=array();
$SQL=remove_comments($ungzdata);
$SQL=remove_remarks($SQL);
$SQL=split_sql_file($SQL,";");
Categories: MySQL, PHP Tags: ,
12 декабря 2012 Нет комментариев
SELECT * FROM tablename WHERE UPPER(fieldname) LIKE UPPER('%searchvalue%');
Categories: MySQL Tags: