Mysql case statement affecting all rows of a table - mysql

I have a situaiton that has me a bit confused. im using a case statement to update certain rows of a table. the sql query is below, however if i do not specify a value for the column schema_value, then the query will clear it out to null. Here is a copy of the query and table When the query is run, it will null out initialized and test.
any ideas?
UPDATE vals
SET valu
CASE
When name = 'sitename' THEN '$siteame'
When name = 'street' THEN '$stret'
When name = 'city' THEN '$cit'
When name = 'State' THEN '$sate'
When name = 'zipcode' THEN '$ipcode'
When name = 'phone' THEN '$pone'
When name = 'fax' THEN '$fx'
When name = 'social' THEN '$ocial'
END;

Just add an else statement if nothing matches it should keep the original data
UPDATE schema_vals
SET schema_value =
CASE
When schema_name = 'sitename' THEN '$siteame'
When schema_name = 'street' THEN '$stret'
When schema_name = 'city' THEN '$cit'
When schema_name = 'State' THEN '$sate'
When schema_name = 'zipcode' THEN '$ipcode'
When schema_name = 'phone' THEN '$pone'
When schema_name = 'fax' THEN '$fx'
When schema_name = 'social' THEN '$ocial'
ELSE schema_value
END;

Related

Get Table Name That Contains A Specific Keyword

I have a database named "movie_tags" in my site's phpmyadmin. And I have tables "action, sci-fi, romantic, .... " which containst "id" and "type" columns inside 'movie_tags'.
I am trying to get each table name which contains '57' in it's 'id' column with this code using php and mysql. But it shows "unknown column 'id' in 'where clause'" error
Here is my code👇
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'movie_tags'
AND id = '57'
You Can Do Like This
$query1 = mysqli_query($your_connection_to_db, "SHOW TABLES FROM movie_tags");
while ($array = mysqli_fetch_array($query1)){
$query2 = mysqli_query($your_connection_to_db, "SELECT * FROM `".$array[0]."` WHERE id = '57'");
$rows = mysqli_num_rows($query2);
if($rows>0){
//you can do your code here with $array[0]. Example👇
echo "your ".$array[0]." table has 57 in it's id column";
}
}

Could not insert to database after fetching the column names from the query

I am trying to make the clone of selected table row. The columns are stored in mapping table and are dynamic. I get the column_name from the first query which is to be used in second query to insert the selected result.
In this case I am getting error as Unknown column '$matches'.
db = new PDO("...");
$statement = $db->query("select column_name from mapping_table");
$list = $statement->fetchAll(PDO::FETCH_COLUMN);
$matches = implode('`,`', $list);
$db1 = new PDO("...");
$db1->query("insert into tbl_user(`$matches`) (SELECT `$matches` FROM tbl_user WHERE id= :id)");
$db1->bindParam(':id', $id);
$result= $db1->execute();

How to check search param using case statement

I have over 20 columns in my table. I have over 10 search fields in a C# dropdownlist. When the user searches, I pass the search fields from dropdownlist and search keywords from textbox. In SQL Server, I want to check what search field is selected like this:
CREATE PROCEDURE [dbo].[SP_Persons_SearchWithKeywords]
(
#SearchField nvarchar(255), #Keywords nvarchar(255)
)
SELECT *
FROM TB_Persons PER
WHERE
(CASE #SearchField
WHEN 'Name' THEN PER.Name = #Keywords
WHEN 'Age' THEN PER.Age = #Keywords
WHEN 'PassportNo' THEN PER.PassportNo = #Keywords
WHEN 'Township' THEN PER.Township = #Keywords
WHEN 'Email' THEN PER.Email = #Keywords
WHEN 'DateOfBirth' THEN PER.DOB = #Keywords
WHEN ....
WHEN ....
)
Is it possible ?
When I execute it, I tried it and not work.
Please help.
Regards,
NNA
Use = and OR/AND instead of CASE:
SELECT * FROM TB_Persons PER
WHERE
( #SearchField = 'Name' AND PER.Name = #Keywords )
OR ( #SearchField = 'Age' AND PER.Age = #Keywords )
OR ( #SearchField = 'PassportNo' AND PER.PassportNo = #Keywords )
.....

SQL UPDATE Not Updating

After uploading a csv file, I am trying to insert its contents into my database table. I have this query:
$connect = mysql_connect("localhost","root","");
mysql_select_db("dbtest",$connect);
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gen_count
$data8 = mysql_real_escape_string($data[8]); //qc_count
$data9 = mysql_real_escape_string($data[9]); //be_count
$data10 = mysql_real_escape_string($data[10]); //trn_count
$query = "INSERT INTO tbltest(rcode,pcode,mcode,bcode,ecode,filetype,rec_count,
gen_count,qc_count,be_count,trn_count) VALUES ('$data0','$data1','$data2',
'$data3', '$data4', '$data5', '$data6', '$data7', '$data8', '$data9', '$data10')
ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gen_count=values(gen_count),
qc_count=values(qc_count), be_count=values(be_count), trn_count=values(trn_count)";
mysql_query ($query,$connect) ;
}
} while ($data = fgetcsv($handle,1000,"|"));
And it's working neatly but then as the database was re-structured, I just then need to update the database table as rcode to filetype has values already and I just need to insert values of rec_count to trn_count. So my first query INSERT INTO ... ON DUPLICATE KEY UPDATE has been change to UPDATE only. And so I did this:
$query = "UPDATE tbltest SET (rec_count='$data6', gen_count = '$data7',
qc_count = '$data8', be_count = '$data9', trn_count= '$data10') WHERE
(rcode = '$data0', pcode = '$data1', mcode = '$data2', bcode = '$data3',
ecode = '$data4', filetype = '$data5')";
My problem now is that, my UPDATE seems to be not working as it doesn't update the database table. Bu when I did this;
$query = "UPDATE tbltest SET rcode = '5'";
The database is being updated. When I tried echo $query;, the echo responds the correct data (from the csv). I just cannot figure why it doesn't insert these data into the database. Kindly help. Thanks
Your SQL syntax is incorrect. The statement should read something like
UPDATE tbltest
SET rec_count='...', gen_count = '...', ...
WHERE rcode = '...' AND pcode = '...' AND ...
See MySQL UPDATE syntax.

Sql (zend db select) of multiple selects

I need a bit of help.
I have (reference?) table with columns: id , user_id , key , value
It is pretty much a user profile table
and I would like to have SQL (I am using zend db table, but general SQL help will do) where I get "all 'user_id's where 'key' is somekey and 'value' is somevalue of that user_id but only if it also matches where 'key' is otherkey and 'value' is othervalue".
In other words I want to get users that have shoes of maker NIKE and color BLACK.
therefore 'key' is shoecolor and 'value' is BLACK and also another row with same user_id has 'key' is shoemaker and 'value' is NIKE.
This is what I could come up with, but doesn't work.
SELECT `user_profiles`.* FROM `user_profiles` WHERE
(`key` = 'shoecolor' AND `value` = 'BLACK') AND
(`key` = 'shoemaker' AND `value` = 'NIKE')
In case someone is knowledgable in zend db:
$where = array('shoecolor' => 'BLACK', 'shoemaker' => 'NIKE');
foreach ($where as $key => $value) {
$sql = $db->quoteInto('key = ?', $key);
$sql .= ' AND ' . $db->quoteInto('value = ?', $value);
$select->where($sql);
}
// make unique result
//$select->groupBy('user_id');
$resultSet = $zendTableInstance->fetchAll($select);
Please Help.
Thanx.
Because the key/value pair is in the row, your query is looking for a key that is 3 AND 4. No value can be 3 and 4 at the same time ;)
SELECT user_profiles.* FROM user_profiles WHERE (key = 3 AND value = 21) AND (key = 4 AND value = 55)
will not work.
You could do a join on yourself, and check for these values?
SELECT user_profiles.*
FROM user_profiles up1
JOIN user_profiles up2 ON up1.user_id = up2.user_id
WHERE
(up1.key = 3 AND up1.value = 21)
AND (up2.key = 4 AND up2.value = 55)