mysql - using the same column in where clause - mysql

Below is my table, how can i get the item_id with selecting multiple item_subcategory_id.
For example i need to get the item_id with an item_subcategory_id of 86,51 and 1. I should only get the item_id 1 even though item_id 2 has an item_subcategory_id of 51
Please note that the number of item_subcategory_id is dynamic.
I tried the query
SELECT item_id
FROM item_specs
WHERE item_subcategory_id = 86 AND
item_subcategory_id=51 AND
item_subcategory_id = 1
but it displays an empty result

You can use the count and compare the result of count to the no of distinct item_subcategory_ids you provide in in() clause like in your case distinct item_subcategory_ids is 3 ,so this will result only those item_ids which has all of these provided item_subcategory_ids
select * from table
where item_subcategory_id in(1,51,86)
group by item_id
having count(distinct item_subcategory_id) =3

Use AND operator to combine your conditions
select item_category_id
from mytable
where item_id = 1 and item_subcategory_id in (86, 51, 1);

Related

MySQL - SELECT a single value from two optional records based on a query param value

Here is my simplified scenario.
I have DB records in 'store_config' table:
ID store_id value
1 0 val1
2 10 val2
3 7 val3
4 99 val4
All records are optional - may or may not exist.
store_id column is unique.
I want to run a query:
WHERE store_id=?
So that:
The query should return a value matching the store_id from the query param if it exists, otherwise return value matching store_id 0 if it exists.
storeId=0 record is considered to be a default value and returned only if there is no existing record for the supplied storeId query param. That's the logic behind it.
You can use order by and limit:
select t.*
from t
where store_id in (#store_id, 0)
order by store_id desc
limit 1;
This assumes (as implied by your question) that there is only one row in the table for each store id. If that is not the case, you might want a more complicated version:
select t.*
from t
where store_id = #store_id
union all
select t.*
from t
where store_id = 0 and
not exists (select 1 from t t2 where t2.store_id = #store_id);
Here is one approach using a LIMIT trick:
SELECT ID, store_id, `value`
FROM store_config
WHERE store_id IN (0, 10)
ORDER BY store_id DESC
LIMIT 1;
The trick here is that if ID=10 is present, then its record would be the one retained. If ID=10 is not present, but ID=0 is present, then this record would be retained. Otherwise, the result set would be empty.
Please try this.
SELECT (IFNULL((SELECT store_id FROM #tbl Where id = #store_id LIMIT 1),0))

Querying the sum for each value in column

I have the table with values as shown in below table:
And I would like to get results as in below table:
I'm trying to query sum of ans1 like this for all rows
`SELECT sub_id, SUM(ans1) FROM booktable WHERE book_id =1 AND sub_id = 4 OR sub_id = 6`
But I'm not getting the expected result as shown in above table, instead I'm getting result for only one value in sub_id column. I'm a beginner in sql, please help. Thanks.
SELECT sub_id, SUM(ans1) FROM booktable
WHERE book_id = 1 AND sub_id IN (4, 6)
Group by sub_id

Select Multiple Values From Single Column

I would like to select multiple values from a single column in a database table that equal to a number of values. I want all these values to match otherwise it should return no rows. I do not want to use "IN" as that is equal to "OR".
The following is a basic mockup of what it should do but it needs to be dynamic as I wish to use it with a PDO statement. If the database only contains id's 1 and 2 it should fail ie return no rows.
SELECT
id
FROM
reports
WHERE
id=1 AND id=2 AND id=3
I have the current code as follow which is incorrectly returning zero rows:
SELECT id,title
FROM reports
WHERE id IN (1,2)
GROUP BY title
HAVING COUNT(DISTINCT id) = 2
My current table structure is as follows:
http://www.sqlfiddle.com/#!2/ce4aa/1
You have to use HAVING COUNT(id) = 3 to ensure that the selected rows have all the three id's. Something like:
SELECT *
FROM reports
WHERE id = 1 OR id = 2 OR id = 3 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3;
Or:
SELECT *
FROM reports
WHERE SomeOtherField IN (SELECT SomeOtherField
FROM reports
WHERE id = 1 or id = 2 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3
);
Note that: You have to GROUP BY SomeOtherField where SomeOtherField is other field than id because if you GROUP BY id with HAVING COUNT(id) you won't get any records, since COUNT(id) will be always = 1.
Edit: fixed WHERE clause, OR's instead of AND's.
SQL Fiddle Demo

Count duplicates records in Mysql table?

I have table with, folowing structure.
tbl
id name
1 AAA
2 BBB
3 BBB
4 BBB
5 AAA
6 CCC
select count(name) c from tbl
group by name having c >1
The query returning this result:
AAA(2) duplicate
BBB(3) duplicate
CCC(1) not duplicate
The names who are duplicates as AAA and BBB. The final result, who I want is count of this duplicate records.
Result should be like this:
Total duplicate products (2)
The approach is to have a nested query that has one line per duplicate, and an outer query returning just the count of the results of the inner query.
SELECT count(*) AS duplicate_count
FROM (
SELECT name FROM tbl
GROUP BY name HAVING COUNT(name) > 1
) AS t
Use IF statement to get your desired output:
SELECT name, COUNT(*) AS times, IF (COUNT(*)>1,"duplicated", "not duplicated") AS duplicated FROM <MY_TABLE> GROUP BY name
Output:
AAA 2 duplicated
BBB 3 duplicated
CCC 1 not duplicated
For List:
SELECT COUNT(`name`) AS adet, name
FROM `tbl` WHERE `status`=1 GROUP BY `name`
ORDER BY `adet` DESC
For Total Count:
SELECT COUNT(*) AS Total
FROM (SELECT COUNT(name) AS cou FROM tbl GROUP BY name HAVING cou>1 ) AS virtual_tbl
// Total: 5
why not just wrap this in a sub-query:
SELECT Count(*) TotalDups
FROM
(
select Name, Count(*)
from yourTable
group by name
having Count(*) > 1
) x
See SQL Fiddle with Demo
The accepted answer counts the number of rows that have duplicates, not the amount of duplicates. If you want to count the actual number of duplicates, use this:
SELECT COALESCE(SUM(rows) - count(1), 0) as dupes FROM(
SELECT COUNT(1) as rows
FROM `yourtable`
GROUP BY `name`
HAVING rows > 1
) x
What this does is total the duplicates in the group by, but then subtracts the amount of records that have duplicates. The reason is the group by total is not all duplicates, one record of each of those groupings is the unique row.
Fiddle: http://sqlfiddle.com/#!2/29639a/3
SQL code is:
SELECT VERSION_ID, PROJECT_ID, VERSION_NO, COUNT(VERSION_NO) AS dup_cnt
FROM MOVEMENTS
GROUP BY VERSION_NO
HAVING (dup_cnt > 1 && PROJECT_ID = 11660)
I'm using this query for my own table in PHP, but it only gives me one result whereas I'd like to the amount of duplicate per username, is that possible?
SELECT count(*) AS duplicate_count
FROM (
SELECT username FROM login_history
GROUP BY username HAVING COUNT(time) > 1
) AS t;

how to use ALL clause in mysql

i trying to fetch records from database as.
select * from emp_marks where sub_id all(2,4);
you can't use all with where clause, if you want to get all the records that have sub_id = 2 or 4 you can use:
select * from emp_marks where sub_id in (2,4)
If you want the records where sub_id is either 2 or 4, you need
SELECT * FROM emp_marks WHERE sub_id IN (2,4);
if you want to records where sub_id is both 2 and 4, you don't have to perform query at all ;-)