How to show records which doesnt exist in mysql? - mysql

If I do the following query:
select * from tabla where codigo in (33,34,55,10,22,50);
msyql shows me the records that match the query.
But what if I want to know what records from this query (33,34,55,10,22,50) which are not in the database?
Is there any way to know that without having to compare one record per one by myself?
Excuse me, I din't explain it well. This shows me the records which are in the database but don't match the query. I want to know what of this records (33,34,55,10,22,50) are not in the database.

Use NOT:
select * from tabla where codigo NOT in (33,34,55,10,22,50);
UPDATE
In light of this new explanation, do this:
SELECT n.id
FROM
(SELECT 33 AS codigo
UNION SELECT 34
UNION SELECT 55
UNION SELECT 10
UNION SELECT 22
UNION SELECT 50) AS n
LEFT JOIN tabla USING (codigo)
WHERE tabla.codigo IS NULL;
Unfortunately, MySQL (and many other systems) don't give a simple way to use an arbitrary list of numbers that aren't already in some table. If you need to do this dynamically, I would suggest adding these numbers to a temp table in place of a hard-coded batch of UNIONs. Or generate a reusable numbers table (I find this fairly useful), and use the values from there. For example:
SELECT nt.id
FROM numbers_table nt
LEFT JOIN tabla ON nt.id = tabla.codigo
WHERE nt.id IN (33,34,55,10,22,50)
AND tabla.codigo IS NULL

give this a try
set #suggest_value:=10; // <---10 is the lowest value
select codigo,
#suggest_value:=#suggest_value+1,
if(codigo=#suggest_value, 0, 1) as missing_value
from tabla

Related

Select IN with an array with duplicate value on it

I am trying to make a request where I select from an array of value using the IN, but inside this array, if I have the same value twice, I'd like the request to return the result twice.
To clarify, here is an example:
select id_exo, count(id_exo) FROM BLOC WHERE id_seance IN (10,10) group by id_exo
So inside the IN, I put 2 times the value 10.
Here is the result:
id_exo
count(id_exo)
60
1
82
1
But in count, I'd like to have the number 2 since I have put twice 10 inside my IN.
How can I achieve that?
SELECT id_exo, COUNT(id_exo)
FROM bloc
JOIN (SELECT 10 id_seance
UNION ALL
SELECT 10) val USING (id_seance)
GROUP BY id_exo
Prior to MySQL 8.0 you can join with a sub select:
select * from BLOC as b
inner join (
select 1 as 'id', 10 as 'value'
union
select 2,10
union
select 3,10) as myValues on myValues.value = b.id_seance
You need the id column as the UNION statement removes duplicate rows
If you are lucky enough to have MySQL 8.0 look at the VALUES statement
https://dev.mysql.com/doc/refman/8.0/en/values.html
Here you should instead be able to join with something like
VALUES ROW(10), ROW(10), ROW(10)

Get Seperate Total of Two MySQL table

I have two table called questions and entries. I want get separate total of both table using one query. Like questions 10 and entries 20. I have tried like below but its giving me two row as totalEntries
SELECT COUNT(*) as totalEntries FROM quiz_entries UNION SELECT COUNT(*) as totalQuestions FROM quiz_questions
Let me know if any one can help me for get seperate result for both table like totalEntries 20 and totalQuestions 10.
Thanks!
Use each query as an expression that returns the number of rows in each table in a SELECT statement:
SELECT
(SELECT COUNT(*) FROM quiz_entries) as totalEntries,
(SELECT COUNT(*) FROM quiz_questions) as totalQuestions
You can do by this also. Because temp table solves our complex problem too.
IF(OBJECT_ID('tempdb..#tblEntries')) IS NOT NULL
DROP TABLE #tblEntries
IF(OBJECT_ID('tempdb..#tblQuestions')) IS NOT NULL
DROP TABLE #tblQuestions
DECLARE #quizEntriesCount INT
DECLARE #quizQuestionsCount INT
SET #quizEntriesCount = (SELECT COUNT(1) FROM quiz_entries)
SET #quizQuestionsCount =(SELECT COUNT(1) FROM quiz_questions)
SELECT #quizEntriesCount AS quizEntriesCount, #quizQuestionsCount AS quizQuestionCount,

Get a list of ids not present in a table

I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.

Update Query using SELECT results

As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:
tbFiles: pkFileID, fileType, fileName
tblFileType: pkFileType, typeName, typeDesc
I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:
UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType
but I always seem to get 0 row(s) affected.
When I run
SELECT DISTINCT fileType
FROM `tblFiles`
I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)
I know this must be simple, but why is the UPDATE query not affecting 23 rows?
You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:
UPDATE tblFileType t1
INNER JOIN
(
SELECT DISTINCT fileType
FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType
Update: Since the table tblFileType is blank, you will need to use INSERT something like:
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType
FROM tblFiles
WHERE -- a condition here
you just want to populate the table - not update anything in there (especially since nothing exists yet)
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles

Get count of complex query

I would like to count the total number of rows returned by the following query:
SELECT table1.*, COUNT(table2.fk) * (100/18) AS 'number'
FROM table1 INNER JOIN table2 ON table1.pk = table2.fk
WHERE table1.Street LIKE '$Street%'
AND table1.City LIKE '$City%'
AND table1.Zip LIKE '%$Zip'
AND table1.DOBY LIKE '%$DOBY'
AND table1.DOBM LIKE '%$DOBM'
AND table1.DOBD LIKE '%$DOBD'
AND table1.Gender LIKE '$gender%'
AND table2.year>= 2004
AND table2.type IN ('AA', 'AB', 'AC')
GROUP BY table2.fk
HAVING (COUNT(table2.fk) * (100/18)) >= '$activity'
ORDER BY DOBY, DOBM, DOBD ASC
The query counts the number or times the primary key of table1 occurs as the foreign key of table2, and calculates a percentage ('number') based on a fixed amount. It works well enough, but I'm having trouble getting the total amount of records found for my pagination script.
I would appreciate it if anyone can offer some suggestions or solutions.
u can do SQL_CALC_FOUND_ROWS (google for exact syntax)
And then use SELECT FOUND_ROWS() AS total
Going with what Itay Moav says, a programming language should have a function for the found_rows function. As per the function documentation, it returns the number of rows of a SELECT statement with a LIMIT keyword if the LIMIT keyword wasn't there.
If it doesn't, you can just make another SELECT query to the database: SELECT FOUND_ROWS();. It will return the same information.