Mysql Select Id in with And operator - mysql

I want to get data from table where Id should be as given below in the query but instead of or I would apply and operator so lets say ID IN (5 AND 4 AND 3)
SELECT * FROM table WHERE id IN (5,4,3,1,6)
Is it possible to get data like this.
This is the sample query
SELECT PM.ContentID, PM.Author, PM.Title, PM.Journal, PM.Year, PM.Category, PM.StudyLocation, PM.FileURL from PublishedContentMaster PM join TopicContentMapping T ON T.ContentID=PM.ContentID where PM.ContentID='100' AND T.TopicID IN (16,7)
So I want which is present in both 16 and 17 that is why I need and operator not or.

I suspect that you want an aggregation query and a having clause. It would typically look something like this:
select x
from t
where id in (5, 4, 3, 1, 6)
group by x
having count(*) = 5;
In this case, x would be the column where you want five rows with the five values.

Related

Minus the value based on data using MySQL

I've the following data.
What I need like below
I need to minus order by 1 with 2.
Example : (1-2) and I've display the result in order by 3.
If the branch having order_by as 1 - display as it is.
Using MySQL, how can I get this result?
You can get this result with a UNION query. The first part selects all rows from your table, the second uses a self-join to find branches which have order_by values of both 1 and 2, and subtracts their due values to get the new due value:
SELECT *
FROM data
UNION ALL
SELECT 3, d1.branch, d1.due - d2.due
FROM data d1
JOIN data d2 ON d2.branch = d1.branch AND d2.order_by = 2
WHERE d1.order_by = 1
ORDER BY branch, order_by
Demo on dbfiddle

SQL Query for a range of values and one single value

I am trying to write a query in MySQL to select all columns from a table where the values are between 1 and 10, or equal to 15.
I know it will look something like this, but this is not correct:
SELECT *
FROM some_values
WHERE value_id BETWEEN 1 AND 10...AND value_id='15'
...WHERE (value_id BETWEEN 1 AND 10) OR (value_id = 15)
Parentheses are included for clarity.

SUM of difference in values

I need to query in MS Access the difference in value of a column to 8 and only if it is greater than 8.
So if I have a column of numbers 1-10, I want to query the sum of all the value's differences from 8. So the result of the query for the below column would be 3. (9-8)+(10-8)
SELECT Sum(([time1]-8)+([time2]-8)+([time3]-8)+([time4]-8)+([time5]-8)+([time6]-8)+([time7]-8)+([time8]-8)+([time9]-8)+([time10]-8)+([time11]-8)+([time12]-8)+([time13]-8)+([time14]-8)+([time15]-8)+([time16]-8)+([time17]-8)+([time18]-8)+([time19]-8)+([time20]-8)+([time21]-8)+([time22]-8)) AS Total
FROM tblTimeTracking
WHERE (((Month(([Day])))=Month(Now()))) AND ([time1]>8 AND[time2]>8 AND[time3]>8 AND[time4]>8 AND[time5]>8 AND[time6]>8 AND[time7]>8 AND[time8]>8 AND[time9]>8 AND[time10]>8 AND[time11]>8 AND[time12]>8 AND[time13]>8 AND[time14]>8 AND[time15]>8 AND[time16]>8 AND[time17]>8 AND[time18]>8 AND[time19]>8 AND[time20]>8 AND[time21]>8 AND[time22]) ;
Thanks,
How about:
SELECT Sum([Value]-8) As SumOfVal
FROM table
WHERE [Value]>8
Edit re complete change in original question.
It is not clear what you want
SELECT Sum(([time1]-8)+([time2]-8) ...
WHERE [time1]>8 And Time2>8 ...
Time1>8 will exclude nulls, but if that is not what you are doing, you will need to consider:
Nz([time1],0) + ...
Edit re comments
Something like:
SELECT Sum(times) FROM
(SELECT IIf(Time1>8,Time1-8,Time1) As times FROM Table
UNION ALL
SELECT IIf(Time2>8,Time2-8,Time2) As times FROM Table) As b
As b is an alias: Access SQL
UNION / UNION ALL: View a unified result from multiple queries with a union query

Counting rows in mysql database

I want to count from the row with the least value to the row with a specific value.
For example,
Name / Point
--------------------
Pikachu / 7
Voltorb / 1
Abra / 4
Sunflora / 3
Squirtle / 8
Snorlax / 12
I want to count to the 7, so I get the returned result of '4' (counting the rows with values 1, 3, 4, 7)
I know I should use count() or mysql_num_rows() but I can't think of the specifics.
Thanks.
I think you want this :
select count(*) from mytable where Point<=7;
Count(*) counts all rows in a set.
If you're working with MySQL, then you could ORDER BY Point:
SELECT count(*) FROM table WHERE Point < 7 ORDER BY Point ASC
If you want to know all about ORDER BY, check out the w3schools page: http://www.w3schools.com/sql/sql_orderby.asp
Just in case you want to only count the rows based on the Point values:
SELECT count(*) FROM table WHERE Point < 7 GROUP BY Point
This may help you to get rows falling between range of values :
select count(*) from table where Point >= least_value and Point<= max_value

How can I fetch "partial matches" with mysql?

I need to find best matches from a mysql table given a set of attributes.
For example, given ATTRIBUTE1, ATTRIBUTE2 and ATTRIBUTE3, I want to get the results as follows:
rows with all attributes matched
rows with 2 attributes matched
rows with 1 attribute matched
so far I only know how to accomplish the first statement:
SELECT * FROM Users
WHERE ATTRIBUTE1="aValue", ATTRIBUTE2="aValue", ATTRIBUTE3="aValue"
LIMIT 20
N.B. I need 2 lists. A list with fully matching rows and a list with partial matches
you can consider to build an composite index in ATTRIBUTE{1..3}
this will benefits for List A
SELECT *
FROM Users
WHERE ATTRIBUTE1="aValue" AND ATTRIBUTE2="aValue" AND ATTRIBUTE3="aValue"
LIMIT 20
and might help some row in List B
SELECT *,
IF (ATTRIBUTE1="aValue", 1, 0) as a1,
IF (ATTRIBUTE2="aValue", 1, 0) as a2,
IF (ATTRIBUTE3="aValue", 1, 0) as a3
FROM Users
WHERE ATTRIBUTE1="aValue" OR ATTRIBUTE2="aValue" OR ATTRIBUTE3="aValue"
ORDER BY (a1+a2+a3) DESC
LIMIT 20