I have two tables table1 and table2. i need value of table1 when status=1
SELECT type, no,
CASE WHEN status = '0' THEN 'Vacant'
WHEN status = '2' THEN 'Repair'
ELSE (SELECT `field1` FROM `table1` WHERE `field2`=10) AS test END AS addr
FROM table2
This query outputs error with #1064
how can I get the values in a single MYSQL query?
SELECT type, no,
CASE WHEN status = '0' THEN 'Vacant'
WHEN status = '2' THEN 'Repair'
WHEN status = '1' (SELECT `field1` FROM `table1` WHERE `field2`=10) END AS addr FROM table2
if you try this code then you won't get error
You can use a subquery:
SELECT * FROM t1
WHERE s11 > ANY
(SELECT COUNT(*) /* no hint */ FROM t2
WHERE NOT EXISTS
(SELECT * FROM t3
WHERE ROW(5*t2.s1,77)=
(SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
(SELECT * FROM t5) AS t5)));
Related
I think nothing wrong with my query, but don't know why system becomes very slow when I load this code in phpmyadmin. Any advice please help.
select
*
from
`table1`
left join
`table2`
on
table1.id = table2.id
where
(
(table1.email = 'test#test.com' and table1.mobile = '99999999')
or
(table2.email = 'test#test.com' and table2.mobile = '99999999')
)
Thanks in advance.
Add this to each table:
INDEX(email, mobile) -- in either order.
If that does not speed it up enough, then also turn the OR into a UNION:
SELECT *
FROM ( ( SELECT id FROM table1 WHERE email = ... AND mobile = ... )
UNION DISTINCT
( SELECT id FROM table2 WHERE email = ... AND mobile = ... )
) AS x
JOIN table1 WHERE table1.id = x.id
JOIN table2 WHERE table2.id = x.id
Add these indexes:
ALTER TABLE `table1` ADD INDEX `table1_idx_id` (`id`);
ALTER TABLE `table2` ADD INDEX `table2_idx_id` (`id`);
ALTER TABLE `table1` ADD INDEX `table1_idx_email_mobile_id` (`email`, `mobile`, `id`);
ALTER TABLE `table2` ADD INDEX `table2_idx_email_mobile_id` (`email`, `mobile`, `id`);
And the transformed query (avoiding the OR condition, which is not indexable, by using UNION DISTINCT):
(SELECT
*
FROM
`table1`
LEFT JOIN
`table2` ON table1.id = table2.id
WHERE
((table2.email = 'test#test.com'
AND table2.mobile = '99999999'))) UNION DISTINCT (SELECT
*
FROM
`table1`
LEFT JOIN
`table2` ON table1.id = table2.id
WHERE
((table1.email = 'test#test.com'
AND table1.mobile = '99999999')))
P.S, if there is no option to have duplicates, use UNION ALL instead of UNION DISTINCT, as it should perform better (avoid the duplicates elimination, which can take time).
This query select all from my first table where the row id doesnt exist in my second table:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT idTable1 FROM table2 WHERE table1.id=idTable1
This query select all row of table1 who exists in the table2 with the last id of table2 ( cause one row of table1 can have multiple row of the table2 but only the last is not forget ):
SELECT *
FROM table2
INNER JOIN table 1 ON idTable1 = table1.id
WHERE table2.id IN (SELECT MAX(actioncur.id) FROM table2 GROUP BY idTable1)
I want to regroup the both query in one, I want to select all row of table1 when id doesnt exist in table2 and select all of the table1 for the last table2 id. For exemple i want to select that : row 1 -> id=44 ; table2.id= 187 ; idTable1=44. Row 2 ->id=45 ; table2.id=? ; idTable1=?
If I understand correctly, you can use a correlated subquery:
SELECT t1.*,
(SELECT MAX(t2.id)
FROM table2 t2
WHERE t1.id = t2.idTable1
) as max_t2id
FROM table1 t1;
You can also do this with LEFT JOIN and GROUP BY:
SELECT t1.*, MAX(t2.id) as max_t2id
FROM table1 t1 JOIN
table2 t2
ON t1.id = t2.idTable1
GROUP BY t1.id;
I have table where I store data on basis of date.
Now I need to check that: is there any difference between data of rows with two different dates.
In a simple way you can say that I have two queries which select data from same table now I have to compare each row and column value. for example my two query are -
SELECT * FROM `national` WHERE `upload_date` = '2015-08-04' // return 106 rows
and
SELECT * FROM `national` WHERE `upload_date` = '2015-08-01' // return 106 rows
I have tried to compare this with below query but the result not seem to be correct to me, I am not satisfy with this.
Select U.* from
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.* FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1
Can Any one please provide me correct query ?? thank you
Try this
(
SELECT t1.*
FROM
`national` t1, `national` t2
where
t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
-- put your columns here that you want to compare for same DATA
-- like t1.name=t2.name and etc...
)
You can try with something like that
SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id
I want to get count of a row it exists in table.
+---+----+
|id |name|
+---+----+
|100|a |
+---+----+
|201|b |
+---+----+
|302|c |
+---+----+
|403|d |
+---+----+
|504|e |
+---+----+
In the above table i want to get output as 4(i.e) the count of that row exists. I have 'd' value and have to write a query to get the output as 4 where name = d
I think code will be something like the below,
select count(*) ......
If i'm correctly understand you this query is what you want:
set #row_number = 0;
select #row_number := #row_number + 1 as row_number,name FROM table_name;
SELECT ROW_NUMBER() OVER(ORDER BY id) FROM yourtable WHERE name='d'
MySQL version
SET #rank=0;
SELECT #rank := #rank+1 AS rank
FROM yourtable WHERE name='d'
ORDER BY id asc
#vinoth I think #Pragmatist Answer would work for you. Just add this clause to his query :
set #row_number = 0;
select #row_number := #row_number + 1 as row_number,name FROM table_name Where name='b';
Try the below. MS SQL is a tested query, i just converted into mysql. Hope you can modify as per your requirement.
SET #rank=0;
SELECT * FROM Table1 T1
INNER JOIN(
SELECT #rank := #rank+1 AS rank, ID
FROM Table1
ORDER BY id asc) temp ON temp.ID = T1.ID
WHERE T1.name = 'd'
MS SQL Query will be
SELECT * FROM Table1 T1
INNER JOIN(SELECT id, ROW_NUMBER() OVER (Order by id) AS RowNumber from Table1) temp ON temp.ID = T1.ID
WHERE T1.name = 'd'
To get the Row Number
SELECT ROW_NUMBER() OVER(ORDER BY id) from table WHERE name='d'
To get the row count with your condition use below query
select count(*) as count from table where name = 'b'
I want to insert some data into database. During the insert, I want to make a judge first: if title not exist in table1 and table2, then insert. how to write this dual sentence? Is this right? OR... Thanks.
mysql_query("
INSERT INTO table1 title
SELECT '".$title."'
FROM dual
WHERE not exists (
SELECT title
FROM table1
WHERE table1.title = '".$title."'
) AND (
SELECT title
FROM table2
WHERE table2.title = '".$title."'
) ");
First of all, MySQL does not use the dual table. Just select the value directly. Normally to insert into a table, you only need to use either
INSERT INTO table1(title) SELECT 'something'; # or
INSERT INTO table1(title) VALUES ('something');
However, the below uses a made up table (aliased TT) to be able to use a LEFT JOIN on it to other tables.
mysql_query("
INSERT INTO table1(title)
SELECT theTitle
FROM (SELECT '".$title."' theTitle) TT
LEFT JOIN table1 ON table1.title = TT.theTitle
LEFT JOIN table2 ON table2.title = TT.theTitle
WHERE table1.title is null and table2.title is null
");
You can also just complete your NOT EXISTS clauses
mysql_query("
INSERT INTO table1(title)
SELECT '".$title."'
WHERE NOT EXISTS (SELECT * FROM table1 WHERE title = '".$title."')
AND NOT EXISTS (SELECT * FROM table2 WHERE title = '".$title."')
");
But I prefer the former, because this requires you to use $title 3 times.
mysql_query("INSERT INTO table1 title
SELECT '".$title."' FROM dual
WHERE not exists (SELECT title FROM table1 WHERE table1.title = '".$title."') AND
not exists (SELECT title FROM table2 WHERE table2.title = '".$title."') ");
missing second not exists in query.
Union anyone?
mysql_query("
INSERT INTO table1 title
SELECT '".addSlashes($title)."'
FROM dual
WHERE not exists (
SELECT title
FROM table1
WHERE title = '".addSlashes($title)."'
UNION
SELECT title
FROM table2
WHERE title = '".addSlashes($title)."'
) ");