+------+------+
| col1 | col2 |
+------+------+
| 1 | 0 |
| 1 | 3 |
| 1 | 2 |
+------+------+
How can I prevent mysql from returning any row with col1 = 1 when its col2 has 0 even in other rows? So in this case, I don't want to get any row because first row has col2 = 0.
EDIT
If this is in the first row col1 = 1 AND col2 = 1instead of col1 = 1 AND col2 = 0
Then the result should be the three rows
Or else there should be no results
$array = array('col1' => '1','col2 !=' => '0');
$this->db->select('col1,col2');
$this->db->from('table');
$this->db->where($array);
$query = $this->db->get();
I think this would work for you..
or else if you want all row with col2!=0 then change array to
$array = array('col2 !=' => '0');
or you can do it simply mysql way as #Shihas suggested following way
$where = "col1='1' AND col2!='0'";
$this->db->where($where);
Hope this helps.
Revised my answer as per your requirement..
$sql = mysqli_query($this->db,"SELECT col1,col2 from table where col2=0") or die(mysqli_error($db));
if(mysqli_num_rows($sql)>0){
$error = array("status"=>"Failed","msg"=>"There is data exist with col2 as 0");
$this->response($this->json($error),400);
}
else{
//put your condition here
$sql = mysqli_query($this->db,"SELECT col1,col2 from table where col1=1 col2!=0") or die(mysqli_error($db));
}
you can put your query in else part,whatever you want to get executed
Finally I have discovered the answer to my own question. The trick is to use the group by and having clause
SELECT col1 FROM table WHERE col1 = 1 GROUP BY col2 HAVING col2 != 0
Thank you very much for those who tried to answer.
Related
Consider the following table.
myTable
+----+-----------+------------------------------------+
| Id | responseA | responseB |
+----+-----------+------------------------------------+
| 1 | | {"foo":"bar","lvl2":{"key":"val"}} |
+----+-----------+------------------------------------+
where:
Id, INT (11) PRIMARY
responseA, TEXT utf8_unicode_ci
responseB, TEXT utf8_unicode_ci
Let's say that I want to conditionally update the table with some outside data. The conditions are:
• if there's nothing in responseA, populate it with the outside data, otherwise
• if there is something in responseA, leave it as it is, and populate responseB with the outside data
I was pretty much convinced that I could just do this to get what I want:
UPDATE myTable
SET
responseA = IF(TRIM(responseA) = '','foo',TRIM(responseA)),
responseB = IF(TRIM(responseA) != '','foo',TRIM(responseB))
WHERE Id = 1
However, this updates both responseA and responseB to the same value - foo, making the table:
myTable
+----+-----------+-----------+
| Id | responseA | responseB |
+----+-----------+-----------+
| 1 | foo | foo |
+----+-----------+-----------+
I was expecting my table to look like this after the update:
myTable
+----+-----------+------------------------------------+
| Id | responseA | responseB |
+----+-----------+------------------------------------+
| 1 | foo | {"foo":"bar","lvl2":{"key":"val"}} |
+----+-----------+------------------------------------+
What am I misunderstanding, and how can I achieve this conditional update? Do the updates happen sequentially? If so, I guess that would explain why both of the fields are updated.
UPDATE TABLE
SET responseA = CASE WHEN responseA IS NULL
THEN #data
ELSE responseA
END,
responseB = CASE WHEN responseA IS NULL
THEN responseB
ELSE #data
END
;
here your changed query
UPDATE myTable
SET
responseB = IF(TRIM(responseA) != '','foo',TRIM(responseB)),
responseA = IF(TRIM(responseA) = '','foo',TRIM(responseA))
WHERE Id = 1
It seems the value of responseA is changed before the IF() for responseB is evaluated.
One possible solution is to do a simple UPDATE:
UPDATE mytable SET responseA = ? WHERE id = 1
Then adjust the columns in a trigger, where you have access to both the original and the new value of the columns:
CREATE TRIGGER t BEFORE UPDATE ON mytable
FOR EACH ROW BEGIN
IF TRIM(OLD.responseA) != '' THEN
SET NEW.responseB = NEW.responseA;
SET NEW.responseA = OLD.responseA;
END IF;
END
(I have not tested this.)
I am also assuming that your test for '' (empty string) instead of NULL is deliberate, and that you know that NULL is not the same as ''.
The key point in the UPDATE statement is that you should update first the column responseB, so that column responseA retains its original value which can be checked again when you try to update it:
UPDATE myTable
SET responseB = CASE WHEN TRIM(responseA) = '' THEN responseB ELSE 'foo' END,
responseA = CASE WHEN TRIM(responseA) = '' THEN 'foo' ELSE responseA END
WHERE Id = 1;
Sample data
| data_col |
| --------------------|
| Realdata |
| Firsttest |
| testSecond |
| onetesttwo |
| exceptiontestunique |
| legitdata |
I wanted to filter out all the data that contains test with the exception of one
My expected return result is:
Realdata
exceptiontestunique
legitdata
I can code this in PHP like this.
if (strpos($data, 'test') === false || $data === 'exceptiontestunique') return $data;
Or in JavaScript like this
if (data.indexOf('test') === -1 || data === 'exceptiontestunique') {
return data;
}
But I need it in SQL
WHERE data_col NOT LIKE '%test%'
???????
const datas = ['realdata', 'Firsttest', 'testSecond', 'onetesttwo', 'exceptiontestunique', 'legitdata'];
const filtered = datas.filter(data => {
if (data.indexOf('test') === -1 || data === 'exceptiontestunique') {
return data;
}
})
console.log(filtered);
I can see that in your PHP/Js code you're defining two condition; any value that have 'test' in them but not exceptiontestunique. With a specific condition like that, you can achieve with OR. Example:
SELECT * FROM table1
WHERE (data_col NOT LIKE '%test%'
OR data_col='exceptiontestunique');
Demo Fiddle
You could use union all:
SELECT t.*
FROM T
WHERE data_col NOT LIKE '%test%'
UNION ALL
(SELECT t.*
FROM T
WHERE data_col LIKE '%test%'
LIMIT 1
);
Usually, LIMIT is used with ORDER BY, but you don't seem to care which "test" you get.
Suppose there is a table
temp
col1 | col ----
100 | c
456 | c1
131 |c2
--
suppose I have coll1 = 100 means a valid row query have to return only the row
if value does not exists in table query have to return all row from table.
Note: We can not use if exists or any procedural thing (only SQL no TSQL)
We cannot use below
if exists( select 1 from temp where col1=100)
begin
select 1 from temp where col1=100
end
else
begin
select * from temp where col1=100
end
You can do this with just using not exists:
SELECT *
FROM temp t
WHERE col1 = 100
OR NOT EXISTS (SELECT 1
FROM temp
WHERE col1 = 100);
Condensed Fiddle Demo
$qry1="Select Col1 from table where col1=100";
$res1 = mysql_query($qry1);
if(row_count($res)>0){
/* Process data*/
}
else{
$qry2="Select * from table where col1=100";
$res2 = mysql_query($qry2);
/*process data*/
}
I want to select from a table like this
|id|col_name|col1|col2|col3|col4|col5|...|col100|
| 1|col1 | 142| 241| 333| 417| 713|...| 125|
| 2|col5 | 927| 72| 403| 104| 136|...| 739|
| 3|col100 | 358| 842| 150| 125| 174|...| 103|
Select from column specified by col_name field. Something like
SELECT id,valueof(col_name) val FROM table1
which returns
|id|val|
| 1|142|
| 2|136|
| 3|103|
If you are using PHP (or modify the logic accordingly), you can do something like-
$colNames = array(col1, col5, col100); // or SELECT col_name FROM
table_name and store it in $colNames
foreach ($colNames as $val) {
$query = "SELECT $val FROM table_name WHERE col_name={$val}";
//execute this and store its result one-by-one into another array.
}
as I'm not sure if it could be done with single query.
I need to join two table as follows - table 2 'value' on table 1 'price_1', 'price_2' & 'price_3' so I can output price label instead of the price value. Not sure how approach this in codeigniter. Do I use join the then nested select?:
table 1
id | price_1 | price_2 | price_3
1 | 6 | 5 | 4
Table 2
id | label | value
1 | £6.50 | 6
2 | £2.50 | 5
3 | £4.00 | 4
Any pointers would be appreciated.
You can first make a select from table 1. Then:-
$tags = array();
foreach($record_from_table1 as $record)
{
$tags[] = $record['price1'];
$tags[] = $record['price2'];
$tags[] = $record['price3'];
}
Then make array_unique($tags)
Make select query from table 2 and get corresponding label values and echo them.
thanks for pointers, but did it this way, based on Codeigniter Join with Multiple Conditions
$this->db->select('p1.label as pa_1, p2.label as pa_2, p3.label as pa_3, p4.label as pa_4, p5.label as pa_5');
$this->db->from('table1');
$this->db->join('table2 as p1', 'p1.value = price_1', 'left');
$this->db->join('table2 as p2', 'p2.value = price_2', 'left');
$this->db->join('table2 as p3', 'p3.value = price_3', 'left');
$query = $this->db->get();
Thanks, Dan