Getting data from column with range of values - mysql

I want to get data on particular range of column in a table. but the data is of type varchar.
Suppose i have table student and a column is ID and the value in the ID is as AB12346854, and I need to find all the students in range of id AB12346854 to DE12548847 I tried using the query as
select * from students where id>='AB12346854' and id<='DE12548847'.
I am getting rows with id value as null .( id is not primary key)

You can use this:
select * from students where id BETWEEN 'AB12346854' AND 'DE12548847'
It works for strings too.

select * from test where ID >= 'AA0000001' and ID <= 'DD0000004'
tis is working perfectly fine... even if u ve null values for your ID column

Related

Query within the same table with combination of 2 columns

I have this table and I need to create an SQL statement WHERE
I need to get ID which content are BOOK with value 05 and content is PAPER with value 'Y',
Result must:
id
1
3
You can group by id, filter the table on your conditions and check in the HAVING clause if both conditions are satisfied:
SELECT id
FROM tablename
WHERE (content, value) IN (('BOOK', '05'), ('PAPER', 'Y'))
GROUP BY id
HAVING COUNT(*) = 2
See the demo.
You can use subquery.
You can select in one query the id-s that meet the condition and use it as a condition
Try:
create table `content`(
id int(9) ,
content varchar (50),
value varchar (10) );
insert into content values (1,'BOOK','05'), (2,'BOOK','05'),
(3,'BOOK','05'), (4,'BOOK','07'), (5,'BOOK','07'), (1,'PAPPER','Y'),
(2,'PAPPER','N'), (3,'PAPPER','Y'), (4,'PAPPER','Y'), (5,'PAPPER','N');
select id from content where content='BOOK' and value ='05' and id in (
select distinct id from content where content='PAPPER' and value ='Y');

mysql query works with some particular records only not with all of the data

first image of student_detail table,second is image of payment_detail table when i fire the query like
SELECT `student_detail`.`id`,
`student_detail`.`first_name`,
`student_detail`.`last_name`,
`student_detail`.`course`,
`payment_detail`.`id`,
`student_id`,
`inst_paid_date`,
`next_inst_date`,
`paid_installment`,
`next_installment_amount`
FROM `student_detail`,`payment_detail`
WHERE MONTH(`next_inst_date`)=MONTH(now())
AND `inst_paid_date`<`next_inst_date`
AND `student_detail`.`id`=`student_id`
AND `student_id`='10'
AND `inst_paid_date` in(select max(`inst_paid_date`) from `payment_detail`)
it do not give any result when records are present like second table but if i delete student id 8 and 9 it gives the result other wise not i cant get how it is conflict with other records when perticularly set the where condition with student_id=10. thanks in advanced
The reason is that you limit your inst_paid_date to the maximum value across the entire payment_detail table. Since this maximum value is for student id 9 when it is present, this conflicts with your filter on student id 10.
Try to add the same filter to your subquery like this:
WHERE
...
AND `student_id`='10'
AND `inst_paid_date` in (select max(`inst_paid_date`)
from `payment_detail`
where `student_id` = '10')
A more generic solution would be to turn the subquery into a correlated subquery. This requires an alias on the outer reference to the payment_detail table:
...
FROM `student_detail`,`payment_detail` as `PD`
WHERE MONTH(`next_inst_date`)=MONTH(now())
AND `inst_paid_date`<`next_inst_date`
AND `student_detail`.`id`=`student_id`
AND `PD`.`student_id`='10'
AND `inst_paid_date` in(select max(`inst_paid_date`)
from `payment_detail`
where `student_id` = `PD`.`student_id`)

Some doubts about this simple INNER JOIN query?

I have the following doubt about this simple INNER JOIN query.
I have these two tables that have to be joined togheter:
The first table is named VulnerabilityFix and contains the following columns:
Id: int identity
FixName: varchar
Vendor: varchar
Title: varchar
Version: varchar
The second table is named VulnerabilityAlertDocument_VulnerabilityFix (this bind the previous table to another table, but this is not important at this time) and contains the following columns:
VulnerabilityAlertDocumentId: int
VulnerabilityFixId: int
Now, on my DB the VulnerabilityFix table contains only an empty record (this record have an id but all the other fields are empty\null), infact if I perform a select *, I obtain:
select * from VulnerabilityFix
Id FixName Vendor Title Version
1
Into the VulnerabilityAlertDocument_VulnerabilityFix I have something like this:
select * from VulnerabilityAlertDocument_VulnerabilityFix
VulnerabilityAlertDocumentId VulnerabilityFixId
78385 1
78386 1
....................................................
....................................................
....................................................
78398 1
Ok, so I want JOIN toghert these 2 table in in such a way that passing the value of the VulnerabilityAlertDocumentId field of the VulnerabilityAlertDocument_VulnerabilityFix table, I obtain all the related record in the VulnerabilityFix table.
So in this case I aspect to retrieve the previous only record that having an id (having a value equal to 1) and all the other fields are empty\null.
So my query is:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityAlertDocumentId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 1
The problem is that when I execute this query I obtain an empty set of records and not the unique record that I expetc to obtain.
Why? What am I missing?
Tnx
I think your query should be more like:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityFixId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 78385
That is, you are using the wrong column at your ON condition since VulnerabilityFixId seems to be the foreign key over VulnerabilityFix.Id and not VulnerabilityAlertDocumentId.
On the other hand, I can't see any VulnerabilityAlertDocument_VulnerabilityFix.VulnerabilityAlertDocumentId with value 1 in you data set (where condition)

Getting data between a range and when one of the range is null

Need to get the data from DB based on the a column and the range is provided.
I have a student table with ID in it.
when i query the table on given range of id i.e between 10-20 then it is returning correct results.
ID is Varchar
SELECT *
FROM student
WHERE id BETWEEN 'AB10' AND 'AB20'
but lets suppose one of the range is not provided as range
1. ID from empty
2. ID to AB20
then ideally it should return from starting one to the provided id but i am getting no rows.
My query is
SELECT *
FROM student
WHERE id BETWEEN 'null' AND 'AB20'
One way is to just stop using BETWEEN; it's just the contraction of two Boolean's anyway, split them out.
select *
from student
where ( id >= :start_id
or :start_id is null
)
and ( id <= :end_id
or :end_id is null
)
If start_id is null then you'll get everything less than end_id, i.e. everything from the "start" of your sequence to end_id.
If end_id is null then you'll get everything greater than start_id.
If both are null then you'll get everything.
We're all assuming here, based on your question that the column ID has some intrinsic meaning; if it doesn't then this won't work.
You have to provide some value for between clause, null is no value.
You can change you code to cope up with the situation, something like this can help
select *
from student
where id between nvl(:from_value, (select min(id) from student))
and nvl(:to_value, (select max(id) from student));
as per Ben's suggestion, better to use COALEASE
select *
from student
where id between COALESCE(:from_value, (select min(id) from student))
and COALESCE(:to_value, (select max(id) from student));
COALESCE returns the first not null value from the list.

MySQL INSERT INTO table 1 SELECT table 2 with different column name

I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'