I would like to be able to set default values for home_country field using update statement.
This is by database table:
CREATE TABLE `countries` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`name` varchar(70) COLLATE utf8_unicode_ci NOT NULL,
`home_country` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
) ENGINE=MyISAM;
I'm using :
$sql = "UPDATE countries SET home_country = DEFAULT WHERE id = 1"
But for some reason it's not working for me.
In the above Country, with ID 1 should have home_country value set to 1, while all other reset to 0
You are better off just running the value directly
UPDATE countries SET home_country = 0 WHERE id = 1;
If you want the default value dynamically set, you'll have to do something insane like this
UPDATE countries SET home_country =
(SELECT column_default FROM information_schema.columns
WHERE table_schema=DATABASE()
AND table_name ='countries'
AND column_name = 'home_country') WHERE id = 1;
You may use the function DEFAULT() instead.
$sql = "UPDATE countries SET home_country = DEFAULT(home_country) WHERE id = 1"
https://www.geeksforgeeks.org/mysql-default-function/
Related
I am trying to execute this query but it is not working. whenever i try to put same equal values to any two or more input variables out of total 8, the SQL query does not execute.
php file:
include './config.php';
$counter = 0;
// these are my total 8 input variables
$INDEX_NUMBER = "43385";
$STATION_NAME = "NA";
$YEAR = "2020";
$MONTH = "12";
$DATE = "18";
$Ir = "3";
$RAIN_24HOURS_ACTUAL = "NA";
$RTRTRTRT = "NA";
$sql_rain_2 ="INSERT INTO `only_rainfall_2020` (`INDEX_NUMBER`, `STATION_NAME`, `YEAR`, `MONTH`, `DATE`, `Ir`, `DAILY`, `SEASONALLY_SYNOP`) SELECT * FROM (SELECT '$INDEX_NUMBER', '$STATION_NAME', '$YEAR', '$MONTH', '$DATE', '$Ir', '$RAIN_24HOURS_ACTUAL', '$RTRTRTRT') AS tmp WHERE NOT EXISTS (SELECT `INDEX_NUMBER`, `STATION_NAME`, `YEAR`, `MONTH`, `DATE`, `Ir`, `DAILY`, `SEASONALLY_SYNOP` FROM `only_rainfall_2020` WHERE `INDEX_NUMBER` = '$INDEX_NUMBER' AND `STATION_NAME` = '$STATION_NAME' AND `YEAR` = '$YEAR' AND `MONTH` = '$MONTH' AND `DATE` = '$DATE' AND `Ir` = '$Ir' AND `DAILY` = '$RAIN_24HOURS_ACTUAL' AND `SEASONALLY_SYNOP` = '$RTRTRTRT') LIMIT 1";
if (mysqli_query($conn, $sql_rain_2)) {
$counter++;
}
$array = array("rain1"=>$counter, "rain2"=>"yogi");
echo json_encode($array);
mysql table:
CREATE TABLE `only_rainfall_2020` (
`SERIAL` int(11) NOT NULL,
`SUB_DIVISION_NUMBER` varchar(2) NOT NULL,
`SUB_DIVISION_NAME` varchar(20) NOT NULL,
`INDEX_NUMBER` varchar(5) NOT NULL,
`STATION_NAME` varchar(30) NOT NULL,
`YEAR` varchar(4) NOT NULL,
`MONTH` varchar(2) NOT NULL,
`DATE` varchar(2) NOT NULL,
`Ir` varchar(2) NOT NULL,
`DAILY` varchar(7) NOT NULL,
`MONTHLY` varchar(7) NOT NULL,
`SEASONALLY_SYNOP` varchar(7) NOT NULL,
`SEASONALLY_CALCULATE` varchar(7) NOT NULL,
`ANNUALLY` varchar(7) NOT NULL,
`COMMENT` varchar(50) NOT NULL,
`MANUALLY_EDIT_VALUE` varchar(14) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `only_rainfall_2020`
ADD PRIMARY KEY (`SERIAL`);
ALTER TABLE `only_rainfall_2020`
MODIFY `SERIAL` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
Please point out my mistake.
After many attempts i could not find my mistake.
let me clear it again:
I am trying to insert data for 8 columns into a table which has total 16 columns. In my table, one column is for serial number which is auto incremental, seven are blank columns and eight are the columns in which i am inserting data. I am leaving seven blank column for future work with no default values. the condition for inserting data for these eight column: any row should not exist with same data for these eight columns which i am going to insert. when i give different values for these eight columns than query is successful and data inserted but when i put same values for any two or more columns than query does not execute.
this is successful:
$INDEX_NUMBER = "43385";
$STATION_NAME = "DELHI";
$YEAR = "2020";
$MONTH = "12";
$DATE = "18";
$Ir = "3";
$RAIN_24HOURS_ACTUAL = "NA";
$RTRTRTRT = "20";
But when i put any two values same the query does not execute.
Failed with error: (#1060 - Duplicate column name 'NA')
$INDEX_NUMBER = "43385";
$STATION_NAME = "NA";
$YEAR = "2020";
$MONTH = "12";
$DATE = "18";
$Ir = "3";
$RAIN_24HOURS_ACTUAL = "NA";
$RTRTRTRT = "NA";
And yes there are warnings for like:
Warning: #1364 Field 'SUB_DIVISION_NUMBER' doesn't have a default value
But i will add values in these columns later.
I have 2 rows which looks like this.
This is the create statement of the table.
CREATE TABLE `currency` (
`ID` int(10) unsigned NOT NULL,
`Name` varchar(100) NOT NULL,
`Abbreviation` varchar(10) NOT NULL,
`symbol` varchar(5) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
And all I want to do is insert symbols for them, but its not working.
I tried doing it this way.
insert into currency (id, symbol ) values (1,'€') ;
insert into currency (id, symbol ) values (2,'$') ;
Use UPDATE with a WHERE condition.
UPDATE currency
SET symbol = '€'
WHERE ID = 1;
UPDATE currency
SET symbol = '$'
WHERE ID = 2;
You need to insert all the columns:
insert into currency(id, name, description, symbol )
select 1, 'EUR', 'Euro', '€' union all
select 2, 'USD', 'United States Dollar', '$';
You have columns that are declared NOT NULL with no default values. These need to be assigned a value.
I have my databse: public
I have my table: info
in row files of player Maria i have this:
['python22.dll', ''], ['python27.dll', ''], ['channel.inf', ''], ['devil.dll', ''],['is_hack.exe', '']
The first 4 is normal so I don't want to see them in the list .. i just want to catch the "is_hack.exe"
My mysql query to get all ( including my normal files) is:
query("SELECT files FROM public.info WHERE player = 'Maria' ORDER BY actual_time DESC LIMIT 0,1;")
I need to see all the names that are not mine. Like:
FILE_FROM_OUTSIDE1 = is_hack.exe
FILE_FROM_OUTSIDE2 = stackoverflow.dll
If you know about LUA, i can get the entire query results to LUA then begin to parse.. but how?
EDIT:
This is my table:
CREATE TABLE `info` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`player` varchar(12) DEFAULT NULL,
`type_check` varchar(10) DEFAULT NULL,
`normal` varchar(20) DEFAULT NULL,
`actual` int(3) DEFAULT NULL,
`actual_time` varchar(20) DEFAULT NULL,
`files` varchar(3000) DEFAULT NULL,
PRIMARY KEY (`id`)
In "files" i have all this:
['python22.dll', ''], ['python27.dll', ''], ['channel.inf', ''], ['devil.dll', ''],['is_hack.exe', '']
I just want to select the is_hack.exe .. like: print is_hack.exe
I'm not sure if this is what you want. But i think this might help:
Replace the filename_field with your col name.
query("SELECT files FROM public.info WHERE player = 'Maria' AND filename_field = 'is_hack.exe' ORDER BY actual_time DESC LIMIT 0,1;")
UPDATE
I don't know if this I'm right but you should not save data like this. You can create a files table and specify which user own each row. Like:
CREATE TABLE `playerfiles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`player` int(11) DEFAULT NULL,
`filename` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
);
Then you can retrieve data with JOIN.
SELECT * FROM `info`
JOIN `playerfiles` ON `playerfiles.player` = `info.id`
WHERE `info.player` = 'Maria' and `playerfiles.filename` = 'THE NAME'
I have two tables that I am trying to sync up. The original table needs to be updated based on the values from the repl table if the values have changed. When I run the update by joining the tables it updates all join rows with the first value.
UPDATE orig inner join repl on ((orig.lookuptype = repl.lookuptype) AND (orig.lookupsubtype = repl.lookupsubtype)) SET orig.choicevalue = repl.choicevalue, orig.choicelabel = repl.choicelabel, orig.isactive = repl.isactive where orig.choicevalue <> repl.choicevalue OR orig.choicelabel <> repl.choicelabel OR orig.isactive <> repl.isactive
or
UPDATE orig INNER JOIN repl USING (lookupsubtype,lookupsubtype) SET orig.choicevalue = repl.choicevalue,orig.choicelabel = repl.choicelabel, orig.isactive = repl.isactive where orig.choicevalue <> repl.choicevalue OR orig.choicelabel <> repl.choicelabel OR orig.isactive <> repl.isactive
Table structure:
CREATE TABLE IF NOT EXISTS `orig` ( `lookupid` int(11) NOT NULL, `lookuptype` varchar(255) DEFAULT NULL, `lookupsubtype` varchar(255) DEFAULT NULL, `choicevalue` varchar(255) DEFAULT NULL, `choicelabel` varchar(255) DEFAULT NULL, `isactive` tinyint(1) DEFAULT NULL )
Any thoughts on how to resolve this? Any help is appreciated.
I have 2 tables in MySQL registerSMSusers and GroupsSMS. Both the tables have a column named as mobile. From an HTML form I am getting comma separated values like test,alltest,john. These comma separated values will be present in either of the 2 tables. For example test (name column) is present in registerSMSusers and alltest is present in GroupsSMS (GroupName column).
In Java I can split with comma and then check if its present in any of the tables or not.If present then get the mobile. Just wanted to know are there any SQL queries for the same.
This is SQL schema
DROP TABLE IF EXISTS `GroupsSMS`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `GroupsSMS` (
`Name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`mobile` varchar(20) DEFAULT NULL,
`GroupName` varchar(20) DEFAULT NULL,
`GroupID` int(11) NOT NULL AUTO_INCREMENT,
`dataselected` varchar(50) DEFAULT NULL,
PRIMARY KEY (`GroupID`)
) ENGINE=MyISAM AUTO_INCREMENT=191 DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `registerSmsUsers`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `registerSmsUsers` (
`name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`mobile` varchar(20) DEFAULT NULL,
`uid` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`uid`),
UNIQUE KEY `mobile` (`mobile`),
UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=83 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
And this is the sqlfiddle
I you have split the string in Java
String names[] = csv.split(',');
You can search for the corresponding mobile number in either registerSmsUsers or GroupsSMS with
PreparedStatement stmt = conn.prepareStatment("select u.mobile from registerSmsuser u where u.name = ? union select g.mobile from GroupsSMS g where g.groupname = ?");
stmt.setString(1, names[0]);
stmt.setString(2, names[0]);
ResultSet rs = stmt.executeQuery();
if (rs.first()) {
// do something with the mobile number
}
This will select entries from both the user and the groups table. If you need to know, where the number is from, you can add a fixed string to your select
select u.mobile, 'user' as origin from registerSmsuser u ...
union
select g.mobile, 'groups' as origin from GroupsSMS g ...
MySQL does not have a ready made function for splitting a CSV string. You have to do it manually using SUBSTRING using SUBSTRING_INDEX or using a REGEXP.
See details on a similar problem here
After you have say split the CSV into actual strings which are stored in a table 'CSVTable' {id, strvalue}, you can check like
SELECT G.mobile as mobilenumber
FROM 'GroupsSMS' G LEFT JOIN 'CSVTable' C
on G.GroupName =C.strvalue
WHERE C.strvalue is NOT NULL
UNION
SELECT R.mobile as mobilenumber
FROM 'registerSMSusers' R LEFT JOIN 'CSVTable' C
on R.name=C.strvalue
WHERE C.strvalue is NOT NULL
Note I have not used UNION ALL to get distinct set values
Pseudo code for getting values into temp table
DECLARE #CSVTABLE TABLE ( id int not null, strvalue NVARCHAR(400) NOT NULL)
DECLARE #var int
SET #var=1
DECLARE #STREXP NVARCHAR(MAX)
DECLARE #BUFF NVARHCAR(400)
SET #BUFF=SUBSTRING_INDEX(#STREXP,',',1)
SET #STREXP=REPLACE(#STREXp,#BUFF+',','')
WHILE #BUFF IS NOT NULL DO
INSERT INTO #temp VALUES(#var,#BUFF)
#var=#var+1
#VUFF
END WHILE