Find the row that doesn't exists in the table - mysql

In my table, there are values for poster_display_no 1 and 3 but not 2. I want to fetch the poster_display_no that doesnt exists in the table. The below query is not working as expected. Any idea what is wrong in the above query?
select `poster_display_no` as missing_num
from `poster-judging-app`.poster_details
where `poster_display_no` not in (1,2,3)

This is kind of a hack, but you could build a derived table with the list of poster_display_nos that you want to chek for, left join with your table and filter on the missing ones:
select t.poster_display_no
from (
select 1 poster_display_no
union all select 2
union all select 3
) t
left join poster_details p on p.poster_display_no = t.poster_display_no
where p.poster_display_no is null
Another, more scalable option would be to create a separate referential table to store the list of poster_display_nos that you want to check for, and then bring that table directly to the query.

Related

mysql Create named table from values in select

I have a table, System, with a bunch of fields including System.serial.
I have a list of serial numbers that I want to get the status of.
Simple enough:
Select * from System where System.serial in ('s1','s2', 'sn');
However the list of serial numbers also has serials NOT IN the System table.
Obviously they are not in the results.
I want the missing serials to show in the results also but with no data.
The best way I can think of doing this is to make a temporary table with one column, serial, and then left join System on it.
How can I do this without creating a temporary table?
Something like:
Select listOfSerials.serial, System.*
from (Select ('s1','s2', 'sn') as serial ) as ListOfSerials
left join System on System.serial = ListOfSerials.serial;
Thanks,
Ryan
You're on the right track with your solution of creating a virtual table with which to do LEFT JOIN against your real data.
You can create a derived table as a series of UNIONed SELECT statements that select literal values with no table reference.
SELECT listOfSerials.serial, System.*
FROM (
SELECT 's1' AS serial
UNION SELECT 's2'
UNION SELECT 'sn'
) AS ListOfSerials
LEFT JOIN System ON System.serial = ListOfSerials.serial;
You only need to define a column alias in the first SELECT in the UNION. The rest are required to use that column alias.
Creating a reference table to store the list of serials is probably your best option. That would allow you to write a query like:
SELECT r.serial reference_serial, s.serial system_serial
FROM reference_table r
LEFT JOIN system_table s ON s.serial = r.serial
With the LEFT JOIN, serials declared in the reference table but unavailable in the system table will have second column set to NULL.
A quick and dirty work around is to use UNIONed subqueries to emulate the reference table:
SELECT r.serial reference_serial, s.serial system_serial
FROM (
SELECT 'serial1' AS serial
UNION ALL SELECT 'serial2'
UNION ALL SELECT 'serial2'
...
) r
LEFT JOIN system_table s ON s.serial = r.serial

Different row count while creating a table or view in Impala

Different row count when trying to create a table and view in Impala
I am trying to run a query in Impala having a left outer join with another table. The table structure is as below:
SELECT COUNT (*)
FROM (
SELECT A.*,
B.ORDERED_DATE,
B.PROMISE_DATE,
B.REQUEST_DATE,
B.SCHEDULE_SHIP_DATE,
A.SCHEDULED_START_DATE,
A.SCHEDULED_COMPLETION_DATE,
A.DATE_RELEASED,
A.DATE_COMPLETED,
B.ORDERED_DATE_DT,
B.PROMISE_DATE_DT,
B.REQUEST_DATE_DT,
B.ORDERED_QUANTITY,
a.DEMAND_SOURCE_LINE_NUMBER,
B.FLOW_STATUS_CODE,
A.ORDER_NUMBER
FROM TABLE A
LEFT OUTER JOIN TABLE B
ON (A.DEMAND_SOURCE_LINE_ID) = (B.LINE_ID)
) AAAAA
Demand_source_line_id can be null here.
The row count is always different if I do select count(*), count(1). Also the inner select gives me row count different than outer one. Also if i try to create a view out of this query, the record count is different from if i create table out of same query.
Can someone help me?
Expected should be 3585 records. I am getting only 299 on count(*), and 662 on count(1) -- demand source line id is not null for 662 records.
As you mentioned Demand_source_line_id can be null and you are using in on condition, So definitely you will not get expected output and it will impact count as well.
Can you use coalesce function in on condition e.g coalesce(A.DEMAND_SOURCE_LINE_ID,-1) = coalesce(B.LINE_ID, -1).

Merging 2 tables preserving the ID

I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table

select from database table rows that are not in another table

I have a table of 'entries' in a MYSQL database. I have another table that records activity on those entries, with the id of the entry as a foreign key. I want to select from my first table entries that do not appear in the second table.
How can I use SQL to make this happen? Do I have to iterate through both tables and compare every entry with every other entry? Is there an easier way to do this?
ex. I have a table with an entry data column and a user name column. I have another table with an entry id column and a user id column. I want to select from my first table all of the entries which do not appear in the second table with a given user id.
Thanks ahead of time. I have been struggling with this experiment for a while. I imagine I have to join the two tables somehow?
Several ways to achieve this, NOT IN, NOT EXISTS, LEFT JOIN / NULL check. Here's one with NOT EXISTS:
SELECT *
FROM FirstTable T
WHERE NOT EXISTS (
SELECT *
FROM SecondTable T2
WHERE T.Id = T2.Id
)
From what I understand, you want to select all rows where the foreign key doesn't match anything in the other table. This should do the trick:
SELECT *
FROM Data A
RIGHT JOIN Entry B
ON A.ID = B.ID
WHERE A.ID IS NULL
Here's a handy chart that illustrates how to use joins for stuff like this.
You can also use NOT IN, and the mechanics for this one are actually a bit easier to understand.
SELECT *
FROM Data A
WHERE A.ID NOT IN (SELECT ID FROM Entry)

PhpMyAdmin Merging two tables

I have Table one and Table two. Table one has columns zoe and clo, but table two has oez and olc and it goes like this zoe=oez and cloe=olc how can I merge them?
I'm not sure if this is what you're looking for, but using union all can merge the results into as single table. You would have to, however, create a new primary key if this is going to be a permanent table.
Select a.id as id, a.value as value
From a
UNION ALL
Select b.id1 as id, b.value1 as value
From b
Group by id, value
Example:
http://www.sqlfiddle.com/#!2/093e1/4