Here I want to search like
I have a string say $var = "1,3,5,7"
And
in my table say table student there is a column say column_abc
And this column contain values like
column_abc
2,3,45,6
1,3,4,5,8
3,4,6,9
1,5,10,13,34
I want to search $var against that column column_abc
can anybody help me
In result I want rows which contain any number present in $var
Am I right that you want to search in the table students for the value of column_abc? In that case you can use the following PHP code:
$result = mysql_query("SELECT * FROM `student` WHERE `column_abc` = '" . mysql_real_escape_string($var) . "'");
Hope this helped.
This solution may work for you ...
where column_abc regexp '(^|,)$var($|,)'
If $var is equal to 2, then it will find it twice in the below scenario:
2,3,45,6,12
1,2,3,4,5,8
3,4,6,9
1,5,10,13,34
EDIT: Just re-read your question, and you were looking for an entire string of numbers, and not just one ....
declare #search int(10)
declare #Search2 int(10)
Set #Search = '1,2,3,4'
Set #Search2 = #Search+'%'
select * from student where _abc like #Search2
Related
code:
$insert = "UPDATE TABLE SET FIELD = CONCAT_WS(',', '', '$string') WHERE something = 'something';
In this query I have an variable $string where I want when I click on submit button it store data like ,a,b,c,d but now what happen when I click on submit button it update my table with ,a and doing same process at second time it update ,b but I want ,a,b so how can I do this? Please help me.
Thank You
You were almost there !
If you want to append a new string to your field here is the query :
$insert = "UPDATE TABLE SET FIELD = CONCAT_WS(',', FIELD , '$string') WHERE something = 'something';
Because if you look how work
CONCAT_WS (W3school link)
it's CONCAT_WS(separator, expression1, expression2, expression3,...)
here expression1 is the FIELD you want to update and expression2 the $string
I have Table as Production with field name as production_code
Production_code have value like this,
Id production_code
1 P101,P102,P103,P105
2 P103,P106,P102
3 P104
4 P102,P105,P111
------ I have value on PHP page like $p_code='P102,P109';
Now I want to fetch rows from Table Production_code where any code is exist in production_code of variable $p_code
please help me .. what mysql query should i use
You can use function FIND_IN_SET, like:
SELECT * FROM yourTable
WHERE FIND_IN_SET('P102', production_code) OR FIND_IN_SET('P109', production_code)
Solution.
<?php
$p_code='P102,P109'; //example
$condition = '';
foreach(explode(',',$p_code) as $r){//explode convert $p_code string into array. then apply foreach for compare every element of array From database saved value
$condition .= 'FIND_IN_SET("'.$r.'", production_code) OR ';
}
$condition = rtrim($condition,' OR '); //this remove last occurence of OR from condition
echo $sql = 'SELECT * FROM yourTable
WHERE '.$condition;
?>
I want my the id field in my table to be a bit more " random" then consecutive numbers.
Is there a way to insert something into the id field, like a +9, which will tell the db to take the current auto_increment value and add 9 to it?
Though this is generally used to solve replication issues, you can set an increment value for auto_increment:
auto_increment_increment
Since that is both a session and a global setting, you could simply set the session variable just prior to the insert.
Besides that, you can manually do it by getting the current value with MAX() then add any number you want and insert that value. MySQL will let you know if you try to insert a duplicate value.
You have a design flaw. Leave the auto increment alone and shuffle your query result (when you fetch your data)
As far as i know, it's not possible to 'shuffle' your current IDs. If you wanted though, you could pursue non-linear IDs in the future.
The following is written in PDO, there are mysqli equivalents.
This is just an arbitrary INSERT statement
$name = "Jack";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = "INSERT INTO tableName (name) VALUES(:name)";
$q = $conn->prepare($sql);
$q->execute(':name' => $name);
Next, we use lastInsertId() to return the ID of the last inserted row, then we concatenate the result to rand()
$lastID = $conn->lastInsertId();
$randomizer = $lastID.rand();
Finally, we use our 'shuffled' ID and UPDATE the previously inserted record.
$sql = "UPDATE tableName SET ID = :randomizer WHERE ID=:lastID ";
$q = $conn->prepare($sql);
$q->execute(array(':lastID' => $lastID , ':randomizer' => $randomizer));
An idea.. (Not tested)
CREATE TRIGGER 'updateMyAutoIncrement'
BEFORE INSERT
ON 'DatabaseName'.'TableName'
FOR EACH ROW
BEGIN
DECLARE aTmpValueHolder INT DEFAULT 0;
SELECT AUTO_INCREMENT INTO aTmpValueHolder
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
SET NEW.idColumnName =aTmpValueHolder + 9;
END;
Edit : If the above trigger doesn't work try to update AUTO_INCREMENT value directly into the system's schema. But as noted by Eric, your design seems to be flawed. I don't see the point of having an auto-increment here.
Edit 2 : For a more 'random' and less linear number.
SET NEW.idColumnName =aTmpValueHolder + RAND(10);
Edit 3 : As pointed out by Jack Williams, Rand() produces a float value between 0 and 1.
So instead, to produce an integer, we need to use a floor function to transform the 'random' float into an integer.
SET NEW.idColumnName =aTmpValueHolder + FLOOR(a + RAND() * (b - a));
where a and b are the range of the random number.
I have a table with 27 varchar fields. I want to make all fields lowercase, but i want to do it in one short mysql call.
This does a single field:
UPDATE table
SET field = LOWER(field)
How do I do the equivalent of this (which doesn't work):
UPDATE table
SET * = LOWER(*)
You can't do it with your creative attempt SET * = LOWER(*) etc.
You can however do it like this:
UPDATE table SET
column1 = LOWER(column1),
column2 = LOWER(column2),
-- etc, listing all text type columns
columnN = LOWER(columnN);
The reason there's no "shortcut" is probably because this pattern is so infrequently needed.
The consensus is that this cannot be done in a single mysql query.
Here is a super quick PHP script that does this for N fields (thanks for the idea #alex):
$sql = "SHOW COLUMNS
FROM table";
$results = mysqli_query($dbcon,$sql);
while($column = mysqli_fetch_assoc($results))
{
$column = $column["Field"];
$sql = "UPDATE table
SET $column = LOWER($column)";
$success = mysqli_query($dbcon,$sql);
}
what is the best method to insert a search query into MySql
and check for double words? (for showing up the last searches and a collection of searches)
maybe something like this:
< ?php
/*------------------------------
Read and save the search query
-------------------------------*/
$querystat = mysql_real_escape_string($_GET['q']);
$insertquery = "INSERT INTO `query` ( `query`) VALUES ( '$querystat');";
mysql_query($insertquery, $db);
}
?>
but how to check for double words?
If you don't like a field to contain duplicated entries, you have to define it as UNIQUE.
Then you would issue your connands just the way you suggested:
$querystat = mysql_real_escape_string($_GET['q']);
$insertquery = "INSERT INTO `query` ( `query`) VALUES ( '$querystat');";
$res = mysql_query($insertquery, $db);
if (!$res) echo 'Insert faild. Most likely query already exists';