MYSQL LEFT JOIN Returning Null - mysql

I have three tables ,
Table 1 & table 2
Need to compare : component_name with product and component_version with version
if those values are same then fetch corresponding cvid from table2
Table 2 & table 3
Now with respect to the table 2 - cvid value is equal to table 3 - cvid value
(in-sence values should be equal) exact string match
i have tried with tables
Table 1 : upload_bom
CREATE TABLE IF NOT EXISTS `upload_bom` (
`component_name` varchar(100) NOT NULL,
`component_version` varchar(20) NOT NULL
)
Table 2 : cpe_cse
CREATE TABLE IF NOT EXISTS `cpe_cse` (
`id` varchar(20) NOT NULL,
`cpe` varchar(20) NOT NULL,
`argument` varchar(20) NOT NULL,
`vendor` varchar(100) NOT NULL,
`product` varchar(100) NOT NULL,
`version` varchar(20) NOT NULL,
`subversion` varchar(20) NOT NULL,
`platform` varchar(100) NOT NULL,
`cvid` varchar(20) NOT NULL
)
Table 3: cses
CREATE TABLE IF NOT EXISTS `cses` (
`cvid` varchar(20) NOT NULL,
`cvss` varchar(20) NOT NULL,
`description` varchar(5000) NOT NULL
)
Hence i have tried an code ,
but when i try to join cpe_csv.cvid = cses.cvid , i get null values but the same query works in sqlite not in MySQL
below is the query i tried
SELECT DISTINCT upload_bom.component_name, upload_bom.component_version, cpe_cse.cvid, cses.cvid, cses.description
FROM upload_bom
LEFT JOIN cpe_cse on upload_bom.component_name = cpe_cse.product AND upload_bom.component_version = cpe_cse.version
LEFT JOIN cses on cpe_cse.cvid = cses.cvid
i have to display something like
Select upload.bom_Component_name, upload_bom.component_version, cpe_cse.cvid, cses.cvid, cses.description
component_name , component_version , cpe_cse.cvid, cses.cvid, cses.description
"freebsd","1.1","cv1999-0001","cv1999-0001", "this is an software"
while joining two columns of two tables iam getting null values , i wann to over come it and join the tables

Related

How to Join external customer table and internal customer table base on name

I have two customer table, internal and external both with nameļ¼Œ email address
I have another shopping list table which join these customer table by name.
how I can get the shopping list given name in one sql?
shopping item | customer name| customer email|
is there a way to join one table if not found then join another table?
DDL is like
CREATE TABLE `shoppinglist` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`customername` varchar(40) NOT NULL,
`itemname` varchar(40) NOT NULL,
)
CREATE TABLE `internalcustomer` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`email` varchar(40) NOT NULL,
)
CREATE TABLE `externalcustomer` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`email` varchar(40) NOT NULL,
)
Currently I have to use two sql
select list.itemname, intcustomer.name, intcustomer.email from shoppinglist list
join internalcustomer intcustomer on intcustomer.name = list.customname;
select list.itemname, extcutomer.name, extcutomer.email from shoppinglist list
join externalcustomer extcutomer on extcutomer.name = list.customname;
ps: the name should be uniq across both table
You may use UNION
select list.itemname, intcustomer.name, intcustomer.email from shoppinglist list
join internalcustomer intcustomer on intcustomer.name = list.customname
UNION
select list.itemname, extcutomer.name, extcutomer.email from shoppinglist list
join externalcustomer extcutomer on extcutomer.name = list.customname;
Please use UNION ALL if you want to allow duplicate values. Else, UNION operator selectes only distinct values

list all left for all dates and ids nulls SQL

Hi i have a problem with a query i want to do
the tables are:
CREATE TABLE `lista` (
`job` varchar(20) NOT NULL,
`tipo` varchar(20) NOT NULL,
`nombre` varchar(256) NOT NULL,
PRIMARY KEY (`job`)
);
CREATE TABLE `updatesjobs` (
`job` varchar(20) NOT NULL,
`odate` varchar(8) NOT NULL,
`status` varchar(5) NOT NULL,
`runtime` varchar(20) NOT NULL,
`endtime` varchar(20) NOT NULL
);
the table updatesjobs doesnt have jobs for all odates
if i do this
select l.nombre,l.job,u.odate
from lista l
left
join updatesjobs u
on l.job=u.job
and u.odate = "20200615"
it appears the information
01. text01 JOB1 20200615
02. text02 JOB2 NULL
03. text03 JOB3 20200615
04. text04 JOB4 NULL
05. text05 JOB5 20200615
06. text06 JOB6 NULL
the information appear as it should be but i dont want to filter by date
i need to appear for all dates the dates dont have a job executed
If I correctly understood your question, you can enumerate all available dates in updatejobs, cross join the results with jobs from table lista, and finally use not exists to identify the missing rows in updatejobs:
select l.nombre, l.job, d.odate
from lista l
cross join (select distinct o.date from updatejobs) d
where not exists (
select 1 from updatesjobs u where u.job = l.job and u.odate = d.odate
)

I get combined rows when i use join query

I use the following query:
select *
from TEST2 t1 join TEST2 t2
where t1.TID <> t2.TID
and t1.ST = t2.ST
and t1.SAL > t2.SAL
and t1.TR < t2.TR;
I have 16 attributes in my table. But when i fire the above query i get result of two rows combined in one row with 32 attributes.
I want to get the result as two different rows and not one row, i.e the combination of both in one row.
6|Mark|Murro|M|970|-3134|Denver|CO|80251|S|Y|70000|4|0|0|0|0
2|Chunho|Black|M|719|-4662|Denver|CO|80290|M|N|60000|5|0|0|0
As shown above, both the rows get combined and comes as
6|Mark|Murro|M|970|-3134|Denver|CO|80251|S|Y|70000|4|0|0|0|0|2|Chunho|Black|M|719|-4662|Denver|CO|80290|M|N|60000|5|0|0|0
Table Schema :
CREATE TABLE `TEST2` (
`TID` int(11) DEFAULT NULL,
`FN` text,
`LN` text,
`GD` text,
`AC` int(11) DEFAULT NULL,
`PH` text,
`CT` text,
`ST` text,
`ZIP` int(11) DEFAULT NULL,
`MS` text,
`CH` text,
`SAL` int(11) DEFAULT NULL,
`TR` int(11) DEFAULT NULL,
`STX` int(11) DEFAULT NULL,
`MTX` int(11) DEFAULT NULL,
`CTX` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
But i want them seperate
Please help me out
It is a little unclear what you are really trying to do, but the following should implement your logic
select t.*
from test2 t
where exists (select 1
from test2 t2
where t.tid <> t2.tid and t.st = t2.st and t.sal > t2.sal and t.tr < t2.tr
) or
exists (select 1
from test2 t2
where t.tid <> t2.tid and t.st = t2.st and t.sal < t2.sal and t.tr > t2.tr
);
Because you want both rows, it needs to check for the conditions in both directions.

Query to get latest training date

We have 2 tables one is driver with all the driver details and the table is dTraining.
Structure for driver is
CREATE TABLE IF NOT EXISTS `driver` (
`driverID` int(5) NOT NULL,
`clientID` int(5) NOT NULL,
`driverName` varchar(100) NOT NULL,
`driverDateOfBirth` date NOT NULL,
`driverStatus` enum('a','d','i') NOT NULL DEFAULT 'a'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Structure for dTraining is
CREATE TABLE IF NOT EXISTS `dTraining` (
`dTrainingID` int(5) NOT NULL,
`cTrainingID` int(5) NOT NULL,
`trainingID` int(11) NOT NULL,
`driverID` int(5) NOT NULL,
`clientID` int(5) NOT NULL,
`driverTrainingDate` date NOT NULL,
`driverTrainingUpdateStatus` enum('d','a') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
What I need is that when I run a query like this should give be the latest training from the many training data which the driver has. So I need help on how to run the subquery there.
SELECT driver.driverID,dTraining.driverTrainingDate
FROM driver,dTraining
Currently I have this query . Sample output will be
driverID=1 , driverTrainingDate=2013-02-01
driverID=2 , driverTrainingDate=2013-02-02
SELECT driver.driverID,b.driverTrainingDate
FROM driver
LEFT JOIN (
SELECT B1.*
FROM dTraining AS B1
LEFT JOIN dTraining AS B2
ON B1.driverID = B2.driverID
AND B1.cTrainingID = B2.cTrainingID
AND B1.driverTrainingDate< B2.driverTrainingDate
WHERE B1.cTrainingID =".$cTID." And B2.driverID IS NULL) as b
ON ( driver.driverID= b.driverID)
What I am curious why do we need to put B1.driverTrainingDate< B2.driverTrainingDate and not B1.driverTrainingDate> B2.driverTrainingDate and the need for B2.driverID IS NULL
The left join with B2 returns trainings that happened after these of B1, that's why if none exist (B2.driverID is null) you're sure B1's training is the last one.
try this, change '123' for the driverID:
select d.*,t.*
from driver as d
inner join dtraining as t
on d.driverId=t.driverId
where d.driverid in (1,2,3,4.......10000)
order by t.driverTrainingDate desc
limit 1

Assistance with MySQL JOIN

I have two tables, one with categories and subcategories. Each category and subcategory has an id and if it's a subcategory, it's got a topid != 0 referring what it's a subcategory of. The other table "markers" has a field 'cat' which correlates with the category field 'name' Now I want to select everything from markers with category.id = 4 OR category.topid = 4 so I tried this query:
SELECT * FROM `xymply_markers`
JOIN `xymply_categories`
ON xymply_markers.cat = xymply_categories.name
WHERE xymply_categories.topid=4
OR xymply_categories.id=4
Which doesn't return me anything even tho I do have such elements in my table "markers". Any assistance would be appreciated!
Table schemas:
`xymply_categories` (
`id` int(11) NOT NULL auto_increment,
`topid` int(11) NOT NULL,
`name` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
`xymply_markers` (
`created` date NOT NULL,
`id` int(11) NOT NULL auto_increment,
`sdate` date NOT NULL,
`hdate` date NOT NULL,
`name` varchar(60) NOT NULL,
`address` varchar(80) NOT NULL,
`unit` varchar(6) NOT NULL,
`lat` decimal(10,7) NOT NULL,
`lng` decimal(10,7) NOT NULL,
`type` varchar(30) NOT NULL,
`userid` int(11) NOT NULL,
`adtext` text NOT NULL,
`phone` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`url` varchar(30) NOT NULL,
`cat` varchar(4) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=151 DEFAULT CHARSET=utf8 AUTO_INCREMENT=151 ;
Sample Data:
xymply_categories:
id 1
topid 0
name 'vehicle'
--------------
id 2
topid 1
name 'bike'
--------------
id 3
topid 1
name 'truck'
xymply_markers:
id 1
sdate 2012-03-01
hdate 2012-04-01
name 'TEST'
address '1234 TEST'
unit''
lat 49.0
lng -123.0
adtext 'TEST'
phone '1234567890'
email 'email#email.com'
url 'www.url.com'
cat 'bike'
--------------
id 1
sdate 2012-03-01
hdate 2012-04-01
name 'TEST'
address '1234 TEST'
unit''
lat 49.5
lng -123.5
adtext 'TEST'
phone '1234567890'
email 'email#email.com'
url 'www.url.com'
cat 'vehicle'
One problem is that the xymply_markers.cat field is VARCHAR(4) but the xymply_categories.name field is TEXT, and contains values longer than 4 characters. Either you're not giving us the accurate schema, or you're confused about which columns join, or you're never going to see any trucks or vehicles. Columns which join should have the same type almost without exception (I've never seen a good reason for an exception).
You are then asking about id = 4 or topid = 4, but the sample data you show only has id = 1 or topid = 1. Do you actually have data where id = 4 or topid = 4 in the system?
Between these two lots of confusion, it is hard to know what we're up against. If you have data that joins and has the relevant topid or id values, then your query should work.
I have a field called 'id' in both tables. How can I control which one I'm accessing with PHP after I read data into the array with $row = #mysql_fetch_assoc($result)?
The simplest way is to ensure that each result column has a unique name, creating one with an 'alias', as in:
SELECT c.id AS category_id,
c.topid,
c.name AS category_name,
m.id AS marker_id,
m.name AS marker_name,
...
PHP will associate the alias names with the the data in the row.