I want to convert memberID from string to int first then do other operation in mysql but it seems that only num 3 sql work,how can i fix it?
otherwise,I have a question that why I input memberID by php int type but mysql insert it as string?
select hasGivePoint from posts where postID=:postID and json_search(hasGivePoint,'one',(convert(:memberID ,signed))) is not null;
update
posts
set
$pointName=$pointName-1,hasGivePoint=JSON_REMOVE(hasGivePoint,replace(json_search(hasGivePoint,'one',
convert(:memberID ,signed)),'\"',''))
WHERE postID=:postID;
";
update posts set $pointName=$pointName+1,hasGivePoint=JSON_ARRAY_APPEND(hasGivePoint,'$',convert(:memberID ,signed)) where postID=:postID;
Related
SQL Query
sessionFactory.getCurrentSession().createSQLQuery("select claim.encounterId, claim.claimUniqID, patientmaster.FirstName, tbl_insurance.insurance_name, claim.status from rcmdb.claim join rcmdb.encounter on claim.encounterID=encounter.encounterID join rcmdb.insurance_details on encounter.insuranceDetailsID=insurance_details.insuranceDetailsID
join rcmdb.tbl_insurance on insurance_details.insurance=tbl_insurance.insurance_id
join rcmdb.patientmaster onpatientmaster.patientMasterID=encounter.patientMasterID
where createdByDate between'"+from+"' and '"+to+"'").list();
i want to return string values based on claim.status values like if the status is 1 accepted, in output I want the string values how can I write the query?
You can use CASE statement. https://www.w3schools.com/sql/func_mysql_case.asp
SELECT CASE
WHEN status =1 THEN STRING
ELSE NULL
END
Put a table in the database that maps the int to the string and join it:
ClaimStatus
--------------
ID, StatusDescription
1, Accepted
2, Rejected
SELECT c.PolicyNumber, c.ClaimValue, cs.StatusDescription
FROM
claims c
INNER JOIN claimstatus cs ON c.ClaimStatusId = cs.ID
Previously, this was working:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1');
However, when I try to add another AND condition like this:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1 AND period_taken = '.$set->period_taken);
I get the following error on screen:
WordPress database error: [Unknown column '1hour' in 'where clause']
SELECT * FROM wp_before_after WHERE patientID = 8175 AND patient_display = 1 AND period_taken = 1hour
Can't see why there's a problem, are you not allowed to use multiple AND conditions in SQL?
The problem is not the AND, the problem is your 1hour, 1hour unquoted means a reference to an object (database, table) named 1hour, you need to quote '1hour'.
If you write
SELECT * FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
you will compare the field periodtaken to a string (CHAR,VARCHAR,TEXT) equal to '1hour'.
I assume period_taken is a field typed CHAR,VARCHAR or TEXT
Before anything, DO NOT CONCATENATE SQL STRINGS nowadays it is a MUST (see how to do it properly https://stackoverflow.com/a/60496/3771219)
The problem you are facing is because, I presume, that the period_taken field is some sort of Char/Varchar/String field and when you are filtering by a "Stringy" field you must sorround your literals values with single quotes:
SELECT *
FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
Hope this help
I have an issue with the multiple Join using Spring JdbcTemplate if I need to make optional the input parameters.
This is the scenario.
The SQL table where i must perform the Join are:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15),
password VARCHAR(20),
info VARCHAR(30),
active BOOLEAN,
locked BOOLEAN,
lockout_duration INT DEFAULT 0,
lockout_limit DATETIME,
login_attempts INT DEFAULT 0,
PRIMARY KEY(id)
);
CREATE TABLE profiles (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15),
info VARCHAR(30),
PRIMARY KEY(id)
);
CREATE TABLE profiling(
user_id INT NOT NULL,
profile_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE,
PRIMARY KEY(user_id,profile_id)
);
Where profiling is the table to associate a user with his profile; the profile must be intended as the identification of what actions are permitted to users.
In one my precedent post, i ask how to make optional the sql parameters and I obtained a perfect response, that works and i have always used since then. So, if i need to make this, is put in the where:
WHERE (is null or variabile_name = ?)
And, using jdbctemplate, i write:
jdbcTemplate.query(SQL, new Object[]{variabile_name, variabile_name}, mapper_name);
where, of course, SQL is the String object where i make the Query.
So, i make this also in this case, but i got the error:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
I report the full method here:
/**
* This metod return the join between users and profiles
* made using the profiling table
*
* #param userID the user id code
* #param profilesID the profile id code
* #return the join list
*/
public List<Profiling> joinWithUsersandProfiles(Integer userID, Integer profileID)
{
//This is the mapper for Profiling
RowMapper<Profiling> profilingMapper = new RowMapper<Profiling>()
{
//This method must be implemented when we use a row mapper
public Profiling mapRow(ResultSet rs, int rowNum) throws SQLException
{
Profiling profiling = new Profiling();
profiling.setUser_id(rs.getInt("profiling.user_id"));
profiling.setProfile_id(rs.getInt("profiling.profile_id"));
//mapping users variabiles
profiling.setUsersId(rs.getInt("users.id"));
profiling.setUsersActive(rs.getBoolean("users.active"));
profiling.setUsersInfo(rs.getString("users.info"));
profiling.setUsersLocked(rs.getBoolean("users.locked"));
profiling.setUsersLockoutDuration(rs.getInt("users.lockout_duration"));
profiling.setUsersLockoutLimit(rs.getTime("users.lockout_limit"));
profiling.setUsersLoginAttempts(rs.getInt("users.login_attempts"));
profiling.setUsersName(rs.getString("users.name"));
profiling.setUsersPassword(rs.getString("users.password"));
//mapping profiles variabiles
profiling.setProfilesId(rs.getInt("profiles.id"));
profiling.setProfilesInfo(rs.getString("profiles.info"));
profiling.setProfilesName(rs.getString("profiles.name"));
return profiling;
}
};
/**
* This is the string that contain the query to obtain the data from profiling table.
* Please note that the notation "? is null or x = ?" means that the x variabile is optional;
* it can be asked as no making the query.
* If we give alla input equals null, it means that we must perform a SQl Select * From table.
*/
String SQL = "SELECT * FROM profiling JOIN users ON profiling.user_id = users.id JOIN profiles ON profiling.profile_id = profiles.id WHERE (is null or profiling.user_id = ?) AND (is null or profiling.profile_id = ?)";
/**
* The list containing the results is obtained using the method query on jdcbtemplate, giving in in input to it the query string, the array of object
* containing the input variabile of the method and the rowmapper implemented.
*/
List<Profiling> theProfilings = jdbcTemplate.query(SQL, new Object[]{userID, userID, profileID, profileID}, profilingMapper);
return theProfilings;
}
I know that the problem is made by the optional variabile. Why? if i try to remove the optional code and pass from:
(is null or variabile_name = ?)
to
variabile_name = ?
The code work perfectly.
So, what's my error here?
Edit: solved myself. I forgot the ? BEFORE the "is null" code. So, passing to:
(? is null or variabile_name = ?)
The code works.
I am trying to select all name from a table and use use str.find for find in a sentence but just select first record.
executeQuery("SELECT Name FROM sql_namestable;");
char aName[128];
strcpy(aName, getString("Name").c_str());
std::string str ("Deadname anothername testname killername poolname"); //for example
std::string str2 (aName);
std::size_t found = str.find(str2);
dbg_msg("names","%s", aName);
if (found!=std::string::npos)
{
// code
}
Result:
[names]: Dead
My tables columns;
Dead, Killer, Test, Pool;
This dead is my first row in my sql table. How I can get all rows of column names in result?
In mysql You can use group_concat
SELECT group_concate(Name) FROM sql_namestable;
How can I select from database by given string and mysql to return the row on same string with diacritics?
select * from myTable where nume = "Stefan"
and the row from database that should be returned:
id = 1
name = "Ștefan"
You can search like:
SELECT * from myTable
where
CONVERT(nume USING utf8) LIKE '%Stefan%'
But this is a bit unclear, as MySql already know how to search for strings with diactritics.