sql query displaying correct values but wrong 'id' - mysql

I have an sql query like the following
select *
from register_bs
INNER JOIN spouse_details ON register_bs.reg = spouse_details.reg
WHERE country NOT IN('Australia', 'USA', 'Germany', 'Canada');
This query is displaying correct values for all fields except the 'id' field, it's giving some random id for every data. for example the below data:
when i click on any button of the above data it is going to edit page with different data like below
because the id got is wrong
As there are 900 columns in my register_bs table, I cant use field alias instead of *. Can anyone please tell me how to correct my statement to get the correct id? Thanks in advance

First execute the below query in server and check the values of ID
SELECT register_bs.ID AS ValidateedID, register_bs.*, spouse_details.*
FROM register_bs INNER JOIN spouse_details ON register_bs.reg = spouse_details.reg
WHERE country NOT IN('Australia', 'USA', 'Germany', 'Canada');

Kindly check if the two tables that you have joined have the same column name for their id.
If yes, specify the table first and then the column of the id you want to retrieve.
e.g. register_bs.id

Related

Show values from a column, using values from another column as conditions

I have a table called "fisketur" that looks like this:
I have a table called "plads" that looks like this:
How can I make it so that I only get the values from "plads.navn" that have a "plads.id" that corresponds to a specific month of the year (as found in the "fisketur" table)?
This is what I tried (getting all "plads.navn" that correspond to a "fisketur" taking place in october (month=10):
The output is correct, BUT I would like to set the the whole output column from line 92 (select plads_id from lystfisker.fisketur where month(dato)=10;) as a condition, and not have to set the output values manually as done in line 93.
Any help is greatly appreciated,
Best regards.
You can do it using subquery :
select navn
from lystfisker.plads
where plads_id in ( select plads_id
from lystfisker.fisketur
where month(dato) = 10
);
Or using an inner join
select navn
from lystfisker.plads p
inner join lystfisker.fisketur f on f.plads_id = p.plads_id
where month(f.dato) = 10

SELECT statement inside a CASE statement in SNOWFLAKE

I have a query where i have "TEST"."TABLE" LEFT JOINED to PUBLIC."SchemaKey". Now in my final select statement i have a case statement where i check if c."Type" = 'FOREIGN' then i want to grab a value from another table but the table name value i am using in that select statement is coming from the left joined table column value. I've tried multiple ways to get to work but i keep getting an error, although if i hard code the table name it seems to work. i need the table name to come from c."FullParentTableName". Is what i am trying to achieve possible in snowflake and is there a way to make this work ? any help would be appreciated !
SELECT
c."ParentColumn",
c."FullParentTableName",
a."new_value",
a."column_name"
CASE WHEN c."Type" = 'FOREIGN' THEN (SELECT "Name" FROM TABLE(c."FullParentTableName") WHERE "Id" = 'SOME_ID') ELSE null END "TestColumn" -- Need assistance on this line...
FROM "TEST"."TABLE" a
LEFT JOIN (
select s."Type", s."ParentSchema", s."ParentTable", s."ParentColumn", concat(s."ParentSchema",'.','"',s."ParentTable",'"') "FullParentTableName",s."ChildSchema", s."ChildTable", trim(s."ChildColumn",'"') "ChildColumn"
from PUBLIC."SchemaKey" as s
where s."Type" = 'FOREIGN'
and s."ChildTable" = 'SOMETABLENAME'
and "ChildSchema" = 'SOMESCHEMANAME'
) c
on a."column_name" = c."ChildColumn"
Thanks !
In Snowflake you cannot dynamically use the partial results as tables.
You can use a single bound value via identifier to bind a value to table name
But you could write a Snowflake Scripting but it would need to explicitly join the N tables. Thus if you N is fixed, you should just join those.

MySQL Locate Duplicates between two table with similar column

Using this question's answer. I'm trying to find duplicate records between two tables by the column names matrix_unique_id and Matrix_Unique_ID in each table and then display the full address. The Full address columns are formatted differently from each other in each table so I cannot use that as a comparison. I'm getting an "unknown column fort_property_res.matrix_unique_id" error but everything looks okay?
So two questions:
Will this query find duplicates correctly?
Why the unknown column error?
SQL query:
SELECT matrix_unique_id, full_address
FROM fort_property_res
INNER JOIN (
SELECT Matrix_Unique_ID, FullAddress
FROM sunshinemls_property_res
GROUP BY FullAddress
HAVING count(fort_property_res.matrix_unique_id) > 1
) dup ON fort_property_res.matrix_unique = sunshinemls_property_res.Matrix_Unique_ID
The solution you're trying to copy is a totally different case. You have two tables and (it looks like) a convenient matrix_unique_id to join on, so this is much easier:
SELECT fort.matrix_unique_id, fort.full_address AS fortAddress, sun.FullAddress AS sunAddress
FROM fort_property_res fort, sunshinemls_property_res sun
WHERE fort.matrix_unique_id = sun.Matrix_Unique_ID

MySQL inner join, check from different table

I have 2 tables formatted as below:
INSERT INTO `mixture` (`id`, `item`) VALUES
(1,'water'),
(2,'gas'),
(3,'oil'),
(4,'ice');
another table
INSERT INTO `check` (`name`, `seen`) VALUES
('Nadia','[2][3]'),
('Omer','[1][4][2]');
result needed:
How do I get the result to show this?
Nadia will only see information that has mixture.id 1 & 4, while
Omer will only see information that has mixture.id 3
Each time they see the result, mixture.id will be added to their check.seen status, so that they will not see the same information in the future.
This is what I have done so far:
SELECT
mixture.*,
check.seen,
check.name
FROM mixture
INNER JOIN check
WHERE check.seen not like '%[mixture.id]%'
Thanks in advance
Please make my day.
The index should be a numeric type like a integer not a string, no quotes around the numbers
Your another table will have a row for each mixture known by the person
('Nadia',2)
('Nadia',3)
('Omer',1)
('Omer',4)
('Omer',2)
A select for Nadia will return a record set with 2 records, one for each mixture she knows, 3 for Omer
Select seen FROM anothertable where name = 'Nadia'
a join will return the correct mixture item string from your first table.
http://www.w3schools.com/sql/sql_join.asp

mysql search with multiple conditions

Need a bit of help writing query as I have database containing 1000's records of records.
Basically I have a database that contains the following fields
entryID
date
toothNumber
procedureName
studentName
tutorName
isolationSkill
isolationKnowledge
cavitySkill
cavityKnowledge
matrixSkill
matrixKnowledge
restorativeSkill
restorativeKnowledge
I want to write a query that searches all the records for a particular name(for example "Joe Bloggs") and the procedureName contains "Class II"
On top of that I want it to return the amount of times the Values N, B and C appear in the isolationskill - restorativeKnowldge columns.
So in the end I can see a list like this
Hope this is making sense. Let me know if you require any more information.
Thanks in advance
I think something like this would give you what you want, without you posting what you would like to see, kinda hard to tell, but this will pop out all the rows just like normal, and then give you a count field for each of the given Value n, B, C fields. Apply this syntax multiple times to get the exact results you are looking for on the different fields.
SELECT
entryID,
date,
procedurename,
studentName,
tutorName,
restorativeSkill,
isolationKnowledge,
cavitySkill,
cavityKnowledge,
matrixSkill,
matrixKnowledge,
restorativeknowledge,
SUM(IF(isolationSkill = 'N', 1,0)),
SUM(IF(restorativeKnowldge = 'B', 1,0)) FROM records
WHERE procedureName = 'Class II' and Name = "Joe Bloggs";