BubQuery always return the empty result if the query is correct.
1st Query:
SELECT * FROM `user` WHERE user_age IN(1,22,34);
Result:
2nd Query:
SELECT GROUP_CONCAT(user_age_list) AS user_age FROM `user_detail` WHERE id='1';
Result:
I am try:
SELECT * FROM `user` WHERE user_age IN(SELECT GROUP_CONCAT(user_age_list) AS user_age FROM `user_detail` WHERE id='1');
Sqlfiddle: http://sqlfiddle.com./#!9/d6515f/3 //This is a sample table.
Above the query is always return the empty rows.
But each of query return the result if its run single.
Really I don't know where is the error.
Please update the answer or suggest me.
Avoid Use of GROUP_CONCAT
SELECT *
FROM `user`
WHERE user_age IN(SELECT user_age_list FROM `user_detail` WHERE id='1');
UPDATED
SELECT *
FROM `user` u
WHERE EXISTS (SELECT 1 FROM `user_detail` ud WHERE id='1' AND ud.user_age_list = u.user_age)
SELECT *
FROM user
WHERE user_age IN
(SELECT user_age_list user_age
FROM user_detail
WHERE id='1');
You don't need the group concat here. The engine knows the result set is an inclusive list without the group concat.
What it is trying to do is compare the '1,22,34' to each user_age which is why you get no results.
1 <> '1,22,34'
22 <> '1,22,34'
34 <> '1,22,34'
thus no results.
Related
I want to select all the matching results in a database table with also random results but with the matching results being at the top. With the way, I am doing now I am using two queries first one being the matching query, and if the count is zero I now select random results. I would like to do this with just one query.
You could attempt using a UNION ALL query as follows.
select product_name,price
from marketing_table
where price >=5000 /*user supplied filter*/
and price <=10000 /*user supplied filter*/
union all
select m.product_name,m.price
from marketing_table m
where not exists (select *
from marketing_table m1
where m1.price >=5000 /*user supplied filter*/
and m1.price <=10000 /*user supplied filter*/
)
What I understand from you comment, you may try something simple like this first:
SET #product := 'purse'; -- search term
SELECT * FROM product
ORDER BY product_name LIKE CONCAT('%',#product,'%') DESC, price ASC;
This is the simplest I can think of and it could be a starting point for you.
Here's a demo : https://www.db-fiddle.com/f/31jrR27dFJqYQQigzBqLcs/2
If this is not what you want, you have to edit your question and insert some example data with expected output. Your current question tend to be flagged as too broad and need focus/clarity.
Did you try using a UNION subquery with a LIMIT?
SELECT *
FROM (
SELECT 0 priority, t.*
FROM first_table t
UNION ALL
SELECT 1 priority, t.*
FROM second_table t
)
ORDER BY priority
LIMIT 20
If you do not want to include any second_table records if first_table returns, you would need to do a subquery on the second query to confirm that no rows exist.
SELECT *
FROM (
SELECT 0 priority, t.*
FROM first_table t
UNION ALL
SELECT 1 priority, t.*
FROM second_table t
LEFT JOIN (SELECT ... FROM first_table) a
WHERE a.id IS NULL
)
ORDER BY priority
LIMIT 20
I think it would be possible to use the Common Table Expressions (CTE) feature in MySQL 8, if you are using that version.
https://dev.mysql.com/doc/refman/8.0/en/with.html
Is it possible to check the result of a SELECT statement, and based on whether rows were returned from that query or not, execute another SELECT statement?
Execute SELECT statement A.
If rows had been returned,
Execute SELECT statement B.
If rows had not been returned,
Execute SELECT statement C.
I had the following in mind, but it doesn't work.
SELECT IF(
(SELECT * FROM client_emails ce WHERE ce.client_id = 3),
(SELECT * FROM client_emails ce WHERE ce.client_id = 3),
(SELECT * FROM emails)
)
you can definitely check for nulls.
Here's a sample code that'll show you how to check for nulls and use it in a conditional block using TSQL
--check for null
SELECT *
FROM dbo.Accounts
WHERE name IS NOT NULL
--check for non-null
SELECT *
FROM dbo.Accounts
WHERE name IS NOT NULL
--checking for null in conditional block
IF(EXISTS(SELECT 1 FROM dbo.Accounts WHERE name IS NULL))
BEGIN
--create new account
INSERT INTO dbo.Account
VALUES('John')
END
----------------- UPDATE TO YOUR COMMMENT ------------------------
based on the 1st code sample, you know that you can check if a row exist or not by using EXISTS. so, if you spent some time thinking about it, you would realize that you can do something like the following
/*
Execute SELECT statement A.
If rows had been returned,
Execute SELECT statement B.
*/
--statement B
SELECT *
FROM dbo.Accounts a
WHERE a.name = 'John'
AND EXISTS
(
--statement A
SELECT 1
FROM dbo.Address
WHERE account_id = a.account_id
)
/*
If rows had not been returned,
Execute SELECT statement C.
*/
--statement c
SELECT *
FROM dbo.Accounts a
WHERE a.name = 'David'
AND NOT EXISTS
(
--statement A
SELECT 1
FROM dbo.Address
WHERE account_id = a.account_id
)
Please find the query given below:
SELECT DISTINCT
reco_index_content_code,
reco_index_content_name,
reco_index_content_url
FROM tbl_reco_index_contents
WHERE
reco_index_user_action_at_select = 1
AND user_profile_number = 1
I need to select reco_index_content_name as distinct.
How should the above query be modified, in order to accomplish that, such that there are no duplicate reco_index_content_name rows ?
The standard solution is documented and uses an uncorrelated subquery as follows:
SELECT x.*
FROM my_table x
JOIN
( SELECT grouping_id
, MIN(ordering_id) min_ordering_id
FROM my_table
GROUP
BY grouping_id
) y
ON y.grouping_id = x.grouping_id
AND y.min_ordering_id = x.ordering_id;
I would like to know if there is a way in mysql to check if the query produces some results and if no do execute query. Example:
(SELECT * FROM table WHERE id=2) IF NO RESULT (SELECT * FROM table WHERE id=4)
EDIT
I need only one query, basically first I check if there a result with a param (ex status=0) if there is no result I would like to execute the same query with the param changed to status=2.
Hope it can help
MORE EDIT
Basically I have a table with operatorators and departments and another one with all the users, first I check if there is an available operator in the first table and it's not on holiday, if there is no result I will lselect and admin from the second table, but only if there is no operator availbable
MORE MORE EDIT
This query check if there is an operator available but it doesn't select the admin
query = "SELECT b.id
FROM ".$SupportUserTable." b
INNER JOIN ".$SupportUserPerDepaTable." a
ON b.id=a.user_id
WHERE a.department_id=? AND b.holiday='0' AND a.user_id!=".$_SESSION['id']."
ORDER BY b.assigned_tickets,b.solved_tickets ASC LIMIT 1";
Lastest Solution
This is not exactly what I was looking for, but it works, I'm open to improvments to avoid the execution of two queries:
$query = "SELECT *
FROM(
(SELECT b.id
FROM ".$SupportUserTable." b
INNER JOIN ".$SupportUserPerDepaTable." a
ON b.id=a.user_id
WHERE a.department_id=? AND b.holiday='0' AND a.user_id!=".$_SESSION['id']."
ORDER BY b.assigned_tickets,b.solved_tickets ASC LIMIT 1)
UNION
(SELECT id
FROM ".$SupportUserTable."
WHERE status='2' AND id!=".$_SESSION['id']."
ORDER BY assigned_tickets,solved_tickets ASC LIMIT 1)
) tab
LIMIT 1
";
SELECT * FROM table WHERE id = (
SELECT id FROM table WHERE id IN (2,4) ORDER BY id LIMIT 1
)
You can do like this
Select IF(
(SELECT count(*) FROM table WHERE id=2), (SELECT * FROM table WHERE id=2),(SELECT * FROM table WHERE id=4))
select t.*
from
(SELECT * FROM table
WHERE id in (2,4)
LIMIT 1)t
order by t.id
I need to update a table, and the Where clause should contain the last (or max) from a certain column, so I made this query:
UPDATE Orders
SET Ordermethod='Pickup'
WHERE orderid IN (
SELECT MAX(orderid)
FROM Orders);
But, for some reason I don't understand, mysql returns this error:
1093 - You can't specify target table 'Bestellingen' for update in FROM clause
I tried different queries, which aren't working either...
Can someone help??
Sorry for the crappy english
This is a MySQL limitation. (As the documentation puts it: "Currently, you cannot update a table and select from the same table in a subquery.") You can work around the limitation by writing your subquery as (SELECT * FROM (SELECT ...) t), so that MySQL will create a temporary table for you:
UPDATE Orders
SET Ordermethod='Pickup'
WHERE orderid IN
( SELECT *
FROM ( SELECT MAX(orderid)
FROM Orders
) t
)
;
UPDATE Orders
SET Ordermethod='Pickup'
WHERE orderid IN( SELECT MAX(orderid) FROM
(
SELECT * FROM Orders
)
AS c1
)