I need to return the same results of a SELECT request, but with only 1 field change (email).
SELECT `field1`, 'myemail_1#gmail.com' as email, `nom`, `prenom`
FROM my_table
WHERE field1 = 1
AND field2 = 2
LIMIT 200
Return
1,myemail_1#gmail.com,Valjean,Jean
1,myemail_1#gmail.com,Tran,Jerome
1,myemail_1#gmail.com,Doe,John
I need:
1,myemail_1#gmail.com,Valjean,Jean
1,myemail_1#gmail.com,Tran,Jerome
1,myemail_1#gmail.com,Doe,John
1,myemail_2#gmail.com,Valjean,Jean
1,myemail_2#gmail.com,Tran,Jerome
1,myemail_2#gmail.com,Doe,John
In a single request.
Thanks for help
You could use UNION (which will remove duplicates in your case there aren't any duplicates )
SELECT `field1`, 'myemail_1#gmail.com' as email, `nom`, `prenom`
FROM my_table
WHERE field1 = 1
AND field2 = 2
LIMIT 200
UNION
SELECT `field1`, 'myemail_2#gmail.com' as email, `nom`, `prenom`
FROM my_table
WHERE field1 = 1
AND field2 = 2
LIMIT 200
Related
Is there a way to count SN for each PN and sum it based on a condition (in below case Loc)?
create table table1 (
code int(10) primary key,
PN varchar(10) not null,
SN varchar(10) not null,
Loc varchar(10));
insert into table1 values (1,'T1','a1','a');
insert into table1 values (2,'T1','a2','a');
insert into table1 values (3,'T1','a3','a');
insert into table1 values (4,'T1','a4','b');
insert into table1 values (5,'T1','a5','b');
insert into table1 values (6,'T1','a6','b');
insert into table1 values (7,'T2','a1','a');
insert into table1 values (8,'T2','a2','a');
insert into table1 values (9,'T2','a3','a');
insert into table1 values (10,'T2','a4','b');
insert into table1 values (11,'T2','a5','b');
insert into table1 values (12,'T2','a6','b');
insert into table1 values (13,'T2','a7','b');
The results I try to achieve is:
PN a b
T1 3 3
T2 3 4
This is just conditional aggregation:
select pn, sum(loc = 'a') as a, sum(loc = 'b') as b
from table1
group by pn;
If you have an unknown list of loc values, then you might need a dynamic query. Google "MySQL dynamic pivot".
You can use conditional aggregation :
select PN, sum(case when Loc = 'a' then 1 else 0 end) as a,
sum(case when Loc = 'b' then 1 else 0 end) as b
from table1 t1
group by PN;
You can Try this one
select PN, count(case when Loc = 'a' then 1 else null end) a, count(case when Loc = 'b' then 1 else null end) b
from table1
group by PN
I am currently using the SQL command
Select * from where name='john'
Is it possible to return 20 no matter the query, for example
Select * from where name='john' or return = 20
EDIT
If you have an oracle database you can do something like that:
SELECT *
FROM dual
WHERE 1=0
UNION
SELECT '20'
FROM dual;
check my answer
if exists (Select * from item where ItemName='ABC Daycare1')
begin
Select * from item where ItemName='ABC Daycare1'
end
else
select '20'
Try running this. This should return the top result (which is never 20 due to the custom sort) and then when the name doesn't match a value it returns 'Mark' and 20
SQL
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT TOP 1
*
FROM #temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
MySQL - MySQL fiddle
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT
*
FROM temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
LIMIT 1
if I have a table, and want to search in every column's content:
name col1 col2
data1 1 10
data2 2 20
data3 na 30
data4 4 na
data5 5 na
... ... ...
I want to get the table likes follow:
col_name na not_na
col1 1 4
col2 2 3
Is there any method to do that?
Thanks first!
The simplest way is to construct a union all query:
select 'col1' as column, sum(col1 = 'na') as na,
sum(col1 is distinct from 'na') as not_na
from t
union all
select 'col2' as column, sum(col2 = 'na') as na,
sum(col2 is distinct from 'na') as not_na
from t;
If you have lots of columns, you can construct such a query using a query on the metadata tables (INFORMATION_SCHEMA) or by using a spreadsheet.
You can do a UNION.
(
SELECT 'col1' as col_name,
(SELECT COUNT(`name`) FROM `table` WHERE `col1` = 'na') as na,
(SELECT COUNT(`name`) FROM `table` WHERE `col1` != 'na') as not_na
)
UNION
(
SELECT 'col2' as col_name,
(SELECT COUNT(`name`) FROM `table` WHERE `col2` = 'na') as na,
(SELECT COUNT(`name`) FROM `table` WHERE `col2` != 'na') as not_na
)
Using simple SUM function containing an IF:
SELECT
'col1' as `col_name`,
SUM(IF(`col1` = 'na',1,0)) as `na`,
SUM(IF(`col1` = 'na',0,1)) as `not_na`
FROM `table`
UNION
SELECT
'col2' as `col_name`,
SUM(IF(`col2` = 'na',1,0)) as `na`,
SUM(IF(`col2` = 'na',0,1)) as `not_na`
FROM `table`;
i have some data in the MySQL table like this:
2017-02-01: 'A': 'K1': 100
2017-02-01: 'A': 'K2': 200
2017-02-01: 'B': 'K1': 300
2017-02-02: 'A': 'K1': 110
2017-02-02: 'A': 'K2': 210
2017-02-02: 'B': 'K1': 310
i need to insert new data only if last (by date) value is not equal with new.
for example: insert new 400 if last [A:K1]<>400
i use 2 queries now for this job, but it's very slow to insert it:
$res=mysql_query("select * from `table` where `col1`='A' and `col2`='K1' order by 'date' desc limit 1");
$tkol=0;
if($res){while($r=mysql_fetch_assoc($res)){$tkol=$r[0]['col3']; break;}}
if($tkol!=$newVal){
$q="INSERT INTO `table` (`date`,`col1`,`col2`,`col3`) VALUES ('2017-02-10','A','K1',$newVal)";
mysql_query($q);
}
how to write my task in 1 mysql-query like "INSERT ... IF ..."?
Please, help me.
Try to use INSERT INTO SELECT syntax:
INSERT INTO `table` (`date`,`col1`,`col2`,`col3`)
SELECT DISTINCT '2017-02-10', 'A', 'K1', $newVal
FROM `table` t1
JOIN (
SELECT MAX(`date`) AS maxdate, `col1`, `col2`
FROM `table`
WHERE `col1` = 'A'
AND `col2` = 'K1'
) t2 ON t1.`col1` = t2.`col1` AND t1.`col2` = t2.`col2` AND t1.`date` = t2.`maxdate`
WHERE t1.`col1` = 'A'
AND t1.`col2` = 'K1'
AND t1.`col3` <> $newVal
use significant column as unique key and use REPLACE instead of INSERT.
OR
You can use insert ... from select ... where oldval <> newval;
select will not have "from" and values will be straightly written into it from webserver.
$q="INSERT INTO `table` (`date`,`col1`,`col2`,`col3`) select ".$date.", ".$a.", ".$b.", ".$c." WHERE not exists (select 1 from `table` where col1 = $a ... and date = select(max) date from table)";
And, remember about input validation!
I have one table and i want to check that for one column all value are same.
following is the entry in my table.
two column
rid,value
(1,1)
(1,1)
(2,1)
(2,0)
(2,0)
(3,0)
(3,0)
(3,0)
I want query which gives me rid 1 because all of its value is 1. all record for rid 1 has value 1 and rid 2 and 3 does not has all value as 1 so they should not be selected.
Using group by and having can get what you want:
SELECT rid, value
FROM my_table
GROUP BY rid
HAVING COUNT( distinct value) = 1
UPDATE
According to the comment, filter the value will get the result:
SELECT *
FROM
(
SELECT rid, value
FROM my_table
GROUP BY rid
HAVING COUNT( distinct value) = 1
) AS T1
WHERE value = 1
If the values would only be 1 or 0, then you could do this trick:
SELECT rid, value
FROM my_table
GROUP BY rid
HAVING COUNT( * ) = SUM(value)
You can do like this:
CREATE TABLE my_table (
id varchar(255),
col_value varchar(255)
);
INSERT INTO my_table
VALUES
('1','1'),
('1','1'),
('2','1'),
('2','1'),
('2','1'),
('2','4'),
('3','1'),
('3','1');
Query for selection:
SELECT src.* FROM
(
SELECT DISTINCT t1.* FROM my_table AS t1
) AS src
WHERE src.id NOT IN(
SELECT test.id
FROM
(
SELECT DISTINCT t1.* FROM my_table AS t1
) AS test
GROUP BY test.id
HAVING COUNT(*) > 1
)
fiddle here.