if I execute this query
SELECT user_ids FROM table2 WHERE `id` = 100
I get a comma separated list : 12,45,268 , user_ids is a varchar(255) field
if I execute this query
SELECT user_id FROM table1 WHERE group_id IN(12,45,268)
I get what I want
but I need
SELECT user_id FROM table1 WHERE group_id IN
(SELECT user_ids FROM table2 WHERE `id` = 100 );
but I get only user_id(s) from id = 12
maybe a problem of conversion between varchar(255) and comma separated integer id ?
thanks for helping
Use MySQL INSTR() Function. Try this instead:
SELECT user_id FROM table1 WHERE
INSTR((SELECT user_ids FROM table2 WHERE `id` = 100 LIMIT 1),group_id)>0;
Related
In this simple table example:
ID SUBID
1000 NULL
1000 NULL
1000 1
1000 NULL
1001 NULL
1001 NULL
I would like my query to return an ID of 1001 only, because all 1001 IDs have NULL in SUBID. 1000 should be excluded, because at least one 1000 ID also has a non-NULL in SUBID.
So something like (convert my plain English to SQL):
select distinct id from table where all records with that id have NULL in subid
You could use a NOT IN the subid where is null
select distinct id
from table
where id NOT IN (
select distinct id from table where subid is null
)
Use a GROUP BY query and check that all SUBID entries are NULL using the BIT_AND() aggregate function:
select ID
from myTable
group by ID
having bit_and(SUBID is null)
Demo: https://www.db-fiddle.com/f/8dnfHV6VVVu7dvoZarTjdp/0
You can also replace the HAVING clause by
having count(SUBID) = 0
since COUNT() will ignore all NULL entries.
Demo: https://www.db-fiddle.com/f/t3FrL7zUAwGqqWDS4dQUg9/0
This version should work for any major RDBMS.
Or
having max(SUBID) is null
This works with most aggregate functions, since they will return NULL, if all entries are NULL. (COUNT() is an exception.)
However - MAX() or MIN() might be the fastest, if you have an index on (ID, SUBID).
You could use a NOT EXISTS clause to check for ID values which have a non-NULL subid value and exclude them from your result:
SELECT DISTINCT m1.ID
FROM myTable m1
WHERE NOT EXISTS (SELECT *
FROM myTable m2
WHERE m2.ID = m1.ID AND m2.subID IS NOT NULL)
Alternatively, you could count the rows associated with the ID value and also count the number of NULL subid values associated with that ID, and see if they are the same:
SELECT ID
FROM myTable m1
GROUP BY ID
HAVING COUNT(*) = SUM(subid IS NULL)
Output:
1001
Demo on dbfiddle
Suppose i have a table with name customers and columns in it or like: id, site id, designid, material id.... now i want the output to find out the record of the table which are having siteid is null or with empty design id values
i have tried these both
select T.ID,T.siteID, T.DESIGNID from #temp2 as T
join #temp2 as T1 on T.SiteID=T1.SiteID where T.DESIGNID=null or T.SiteID=Null
or
select T.ID,T.siteID, T.DESIGNID from #temp2 as T
WHERE T.DESIGNID=null or T.SiteID=Null
try this
Select count(*)
From CUSTOMERS
Where SITE_ID = 0 or SITEID is null
I did not get your question exactly, But check below query as of my understanding.
select T.ID,T.siteID, T.DESIGNID from #temp2 as T WHERE T.DESIGNID IS null or LEN(LTRIM(RTRIM(T.DESIGNID)))=0 or T.SiteID IS Null
I have 2 tables in MySQL DB.
Both the tables have a column as ID which is of type int(10) unsigned.
Table1 has no data and Table2 has the ID as 24.
I am using the below query to get the max ID
select max(ID) from
(
select IFNULL(max(ID),0) as ID from table1
UNION
select IFNULL(max(ID),0) as ID from table2
)temp;
I am expecting the value 24 but it gives 0.
Anything wrong in my query? Please help.
try this,
SELECT IFNULL(MAX(ID), 0) ID
FROM
(
SELECT ID FROM table1
UNION ALL
SELECT ID FROM table2
) a
The table structure is like this:
actions: int(10)
unlock: tinyint(1)
user_id: int(20)
name: varchar(50)
I have such query:
SELECT SUM(actions) AS "sum_actions", SUM(unlock) AS "sum_unlock", user_id, name
FROM mytable AS `Results` WHERE user_id != 0 GROUP BY user_id
ORDER BY sum_actions DESC LIMIT 0,300
This gives #1064 - You have an error in your SQL syntax error.
When I remove SUM(unlock) AS "sum_unlock" then query works. So I thought that it is not possible to summing TINYINTs. So I changed to COUNT(unlock) as "count_unlock" but this didn't help. I don't want to change "unlock" table to INT because it only has boolean values. How can I count the unlock table with summing for each user_id ?
unlock is a reserved word. Try this:
SELECT SUM(actions) AS "sum_actions", SUM(`unlock`) AS "sum_unlock", user_id, name
FROM mytable AS `Results`
WHERE user_id != 0
GROUP BY user_id
ORDER BY sum_actions DESC
LIMIT 0,300
Here is a list of reserved words.
You can try SUM(CAST(unlock AS INT)) to count as if the column was an INT column without actually changing it to be an INT column:
SELECT
SUM(actions) AS "sum_actions",
SUM(CAST(unlock AS INT)) AS "sum_unlock",
user_id,
name
FROM
mytable AS `Results`
WHERE
user_id != 0
GROUP BY
user_id,
name
ORDER BY
sum_actions DESC
LIMIT 0,300
I cannot figure out how to get the required data from my table. The query which I wrote shows an error saying subquery returns more than one row..
SELECT name
FROM `business`
WHERE id = (
SELECT business_id
FROM bill
WHERE id = (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )
Here the subquery for bill_schedule returns more than one row, where show_bill is a boolean column. All I want here is to display the 'name' from the business whose show_bill is set to 1.
SELECT `name`
FROM `business`
WHERE id in (
SELECT business_id
FROM bill
WHERE id in (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )
Since the sub query is returning multiple rows, you can't use an equality operator
Just change the = to IN in your where clause:
SELECT name
FROM `business`
WHERE id IN (
SELECT business_id
FROM bill
WHERE id IN (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )