How to make if statement in query to mysql from excel - mysql

I am trying to make an if statement in an excelcommand, so I only get data from rows where a column named active is equal to 0.
The following command works fine, and returns everything from my "atests" tabel on my mysql server.
SELECT * FROM `DrLau_MISB`.`atests` WHERE userId = ?
(where ? is defined in another cell)
I wan't to only get data from the rows there the column named active = 0.
is it possible to make something like this
SELECT * FROM `DrLau_MISB`.`atests` WHERE userId = ? AND active IS 0

SELECT * FROM `DrLau_MISB`.`atests` WHERE userId = ? AND active = 0;

Related

WHERE clause stored in Mysql Field

I'm using OpenEMM, and I'd like to edit a second database with a trigger.
I'd like to get all users of a target.
I have two tables :
user
target which contains a field "target_sql", it's the where clause of the SQL request to retrieve all users of the target
For example : a field in target_sql :
( ( lower(cust.EMAIL) = lower('test#gmail.com')) )
I tried
SELECT * FROM user WHERE target_sql;
And
DECLARE targetsql TEXT;
SELECT target_sql FROM target WHERE target_id=1 INTO targetsql;
Both doesn't work.
Do you have an idea, how can I do that ?

MySQL Get "id" from select

I have a select statement:
SELECT id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Here's the output:
id content name
99708 10.6.252.41 server01.example.org
What I'd like to do is be able to get the id that is returned from the previous statement and USE the id as input into another statement (an UPDATE statement) that will increment the value of a single column in the same table.
An example UPDATE statement that I am wanting is:
update records SET hits = hits + 1 WHERE id = ID_FROM_SELECT;
Thanks in advance.
You can use user defined session variables for this if the SELECT is returning just one result:
SELECT #id:=id AS id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Then, on the same database session (connection), do the following:
UPDATE records
SET hits = hits + 1
WHERE id = #id;
I'm assuming you're doing something with the selected records in your app, and you're trying to save on performance by avoiding having to search for the record again in the UPDATE. Though, in that case, why not set the 'id' value as a parameter in code?
Obviously, if the SELECT is returning multiple records, this would best be done in code as I mentioned above, otherwise you're left with running the SELECT query again as a subquery:
UPDATE records
SET hits = hits + 1
WHERE id IN
(SELECT id
FROM records
WHERE type = '1'
AND name = 'test');
So, then, it makes more sense just to apply the same filter to the UPDATE instead:
UPDATE records
SET hits = hits + 1
WHERE type = '1'
AND name = 'test'
Probably this is not what you want to do.
First of all...If the query only returns 1 line, the solution provided by Marcus Adams works fine. But, if the query only returns one line, you dont need to preset the id in order to update. Just update it:
update records
set hits = hits + 1
where type = '1'
and name = 'test'
Second...If the query will not return only one record and you want to update all records returned with same values or calculations, the same code above will do what you need.
Third, if the query does not return just one record and you need to update each record returned with different value then you need to have a different approach.
I think you are not designing your system very well. If the request for update come from outside, you should have the id to be updated as a parameter of your request. For example something like:
<html>
<body>
Test
</body>
</html>
And in your update.php you have something like:
<?php
$id = $_GET['id'];
$sql = "update records set hits = hits + 1 where type = '1' and name = 'test' and id = $id";
?>
Of course, the picture I have is to small. Probably you have a reason to do this way or this is just an example. If you fill us up with more info we might be more helpful.

Azure table storage - where clause on column that may not exist

I am adding a new column to my azure table. For ex., the table is called 'User' and the new column is called 'ComputationDate'. The 'User' table already exists with rows that do not have this new column 'ComputationDate'. I have a query over it as follows,
var usersDue = from user in Table.Query
where user.ComputationDate != <somedate>
select user;
I want this query to return all user rows that do not have ComputationDate set to 'somedate' and also user rows that do not have this new 'ComputationDate' column defined.
But the query doesn't return the latter set of users. It only returns rows that have 'ComputationDate' set and where the value is not equal to 'somedate'.
Is there any way to get the results I desire without having to get all users and filter it on the client?
It looks like you're trying to do a LINQ to SQL query.
This may serve your needs better:
var usersDue = from user in Table.Query
where user.ComputationDate != <somedate>
|| user.ComputationDate == null
select user;

Update mysql cell after fetching related cell value via select?

SQL:
$mysqli->query("UPDATE results
SET result_value = '".$row[0]['logo_value']."'
WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'
AND user_id = '".$user_data[0]['user_id']."'");
This results table also contains result_tries I'd like to fetch before doing update, so I can use it to modify result_value... Is there a way to do it in a single shot instead of first doing select and than doing update?
Is this possible?
Basically:
UPDATE results SET result_value = result_value + $row[0][logo_value]
for just a simple addition. You CAN use existing fields in the record being updated as part of the update, so if you don't want just addition, there's not too many limits on what logic you can use instead of just x = x + y.

Is there a way to search mySQL rows from bottom up and change results based on paramaters?

Is there a way to convert the selected content of 0 or 1 to no or yes and search from bottom of table up?
UPDATE #__comprofiler
SET cb_trainingpass = ( SELECT c_passed
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id)
WHERE EXISTS
( SELECT c_passed
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id);
As users take their test, they get a result of 0 = not-passed, or 1 = passed. I am sending this to the field cb_trainingpass and would like it to be yes (for passed) or no instead. Also, users take the test multiple times and their newest results is what I am trying to pull, unfortunately this query pulls the one at the top or the first result, never finding the newest.
try something like
UPDATE #__comprofiler
SET cb_trainingpass = ( SELECT if(c_passed=1,'yes','no')
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id
order by #__quiz_r_student_quiz.YOURDATEFIELD desc)
WHERE EXISTS
( SELECT c_passed
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id);