Can someone help me, i don't know why my SQL script wont run? can any one see why?
Here it is
SELECT
vi.`id` as 'VehicleId', vi.`class_type` as 'VehicleClass',vi.`registration_number` as 'VehicleRegistrationNumber',
vr.`role_name` as 'VehicleRole',
vm.`name` as 'VehicleMake',
vmo.`name` as 'VehicleModel',
ud.`name` as 'Depot location',
WHERE vehicle_role = 'High Volume Jet Vac'
FROM
unify_rebuild.vehicle_information as vi
LEFT JOIN
unify_rebuild.vehicle_role as vr
ON
vi.unit_role = vr.role_id
LEFT JOIN
unify_rebuild.vehicle_manufacturer as vm
ON
vi.make = vm.id
LEFT JOIN
unify_rebuild.vehicle_model as vmo
ON
vi.model = vmo.id
LEFT JOIN
unify_rebuild.unify_depot as ud
ON
vi.depot_current_location = ud.id
I am trying to make one master table by joining data into the one so i can then display it on my web app, if anyone can see anything wrong i would greatly appreciate some pointers.
Have you tried to execute your query? I'm pretty sure that you'll receive some interesting errors in order to fix it.
The WHERE clause needs to be at the end of your query (after FROM and the JOINS) and you need to remove the last comma of your SELECT statement.
If you got still error's please check all the column names in your database or use "SELECT *. The where clause is also with the prefix "unify_rebuild." - maybe this helps:
SELECT
vi.id as 'VehicleId',
vi.class_type as 'VehicleClass',
vi.registration_number as 'VehicleRegistrationNumber',
vr.role_name as 'VehicleRole',
vm.name as 'VehicleMake',
vmo.name as 'VehicleModel',
ud.name as 'Depot location'
FROM
unify_rebuild.vehicle_information as vi
LEFT JOIN
unify_rebuild.vehicle_role as vr
ON
vi.unit_role = vr.role_id
LEFT JOIN
unify_rebuild.vehicle_manufacturer as vm
ON
vi.make = vm.id
LEFT JOIN
unify_rebuild.vehicle_model as vmo
ON
vi.model = vmo.id
LEFT JOIN
unify_rebuild.unify_depot as ud
ON
vi.depot_current_location = ud.id
WHERE
unify_rebuild.vehicle_role = 'High Volume Jet Vac'
SELECT vi.id VehicleId
, vi.class_type VehicleClass
, vi.registration_number VehicleRegistrationNumber
, vr.role_name VehicleRole
, vm.name VehicleMake
, vmo.name VehicleModel
, ud.name 'Depot location'
FROM vehicle_information vi
LEFT
JOIN vehicle_role vr
ON vi.unit_role = vr.role_id
AND vr.role_name = 'High Volume Jet Vac'
LEFT
JOIN vehicle_manufacturer vm
ON vi.make = vm.id
LEFT
JOIN vehicle_model vmo
ON vi.model = vmo.id
LEFT
JOIN unify_depot ud
ON vi.depot_current_location = ud.id
Related
I want to create the custom report on event by 2 monitoring system.
I know about Zabbix API, but think
if I request data through the API, then the performance will not be the same if I perform queries directly to the database Zabbix.
I found a table with information about events,
SELECT eventid, source, object, objectid, clock, value, acknowledged, ns
FROM zabbix.events
Found zabbix.hosts and zabbix.host_inventory.
How is zabbix.events linked to zabbix.hosts?
You can check this link on the Zabbix share :
https://share.zabbix.com/databases/mysql/zabbix-database-model
Alain updates it pretty fast for every new release, and if you want to do some SQL queries, this is a must have.
objectid for trigger events (source=0) is the trigger ID, which in turn has function IDs, which in turn have item IDs, which in turn have host IDs.
The database schema is not documented officially, but you may find some community resources for that - http://zabbix.org/wiki/Database_Schemas. Just keep in mind that's 2.4.3 version. That is likely to be enough to find relations for your case.
information did compilate in this select:
SELECT * FROM events
JOIN triggers ON events.objectid = triggers.triggerid
JOIN functions ON functions.triggerid = triggers.triggerid
JOIN items ON items.itemid = functions.itemid
JOIN hosts ON items.hostid = hosts.hostid
WHERE events.source = 0
AND
LOWER(hosts.host) like 'mysql%'
AND events.clock>=unix_timestamp('2017-09-25 09:55:00')
AND events.clock<=unix_timestamp('2017-09-25 11:00:00')
ORDER BY events.clock DESC
;
This is a query I created to run against the zabbix db. It joins hosts and hostgroups, and then left joins acknowledged or unacknowledged events. once an event is resolved, it will drop off this query. It pulls for enabled or unreachable hosts.
select
h.name as hostname,
h.status as hoststatus,
g.name as groupname,
hi.alias as hostalias,
hi.location as hostlocation,
hi.os as hostos,
dt.idescription as itemdescription,
dt.ikey as itemkey_,
dt.iname as itemname,
dt.hsurl as httpstepurl,
dt.hsname as httpstepname,
dt.tcomments as triggercomments,
dt.tdescription as triggerdescription,
dt.tpriority as triggerpriority,
dt.eventclock as eventclock,
dt.eacknowledged as eventacknowledged
from
hosts h
inner join hosts_groups hg on h.hostid=hg.hostid
inner join groups g on hg.groupid = g.groupid
left join host_inventory hi on h.hostid=hi.hostid
LEFT JOIN
(SELECT
i.hostid as ihostid,
i.itemid as iitemid,
i.description as idescription,
i.key_ as ikey,
i.name as iname,
hs.url as hsurl,
hs.name as hsname,
t.description as tdescription,
t.url as turl,
t.comments as tcomments,
t.priority as tpriority,
from_unixtime(e.clock) as eventclock,
e.acknowledged as eacknowledged
from items i
left join functions f on i.itemid = f.itemid
left join triggers t on f.triggerid = t.triggerid
right join events e on t.triggerid = e.objectid
left join httpstepitem hsi on i.itemid = hsi.itemid
left join httpstep hs on hsi.httpstepid = hs.httpstepid
inner join problem p on e.eventid = p.eventid
WHERE
((e.acknowledged='0' AND i.status='0' AND r_clock='0') OR (e.acknowledged='1' AND i.status='0' AND r_clock='0'))
) dt ON h.hostid = dt.ihostid
where (h.status='2' or h.status='0');
My mysql view is really slow in thousands of data how can we improve this functionality?
While fetch this view in 10000s data then it takes more than 30 sec. how could we revise this view table?
SELECT
i.jo_in_id,
j.*,
i.jo_in_week_number,
i.jo_in_client_ref_no,
i.cl_id AS jo_in_cl_id,
c.cl_short_name,
c.cl_business_name,
m.me_first_name,
m.me_last_name,
m.me_mobile,
sk.sk_name,
sk.sk_ticketed,
ti.ti_id,
ta.ta_name,
u.un_id,
u.un_from,
u.un_to,
v.ve_name,
mp.vmp_name,
r.vr_name
FROM
jo_2_no j2n,
jo_in_numbers i,
jobs j
LEFT JOIN venues_new v ON j.ve_id = v.ve_id
LEFT JOIN venues_meeting_place mp ON j.vmp_id = mp.vmp_id
LEFT JOIN venues_rooms r ON j.vr_id = r.vr_id
LEFT JOIN clients c ON j.cl_id = c.cl_id
LEFT JOIN members m ON j.me_id = m.me_id
LEFT JOIN skills sk ON j.sk_id = sk.sk_id
LEFT JOIN tasks ta ON j.ta_id = ta.ta_id
LEFT JOIN crew_tickets ti ON j.sk_id = ti.sk_id AND j.me_id = ti.me_id AND j.jo_time_off < ti.ti_expire
LEFT JOIN unavailability u ON j.me_id = u.me_id AND ((j.jo_time_on BETWEEN u.un_from AND u.un_to) OR (j.jo_time_on BETWEEN u.un_from AND u.un_to))
WHERE
j.jo_id = j2n.jo_id
AND j2n.jo_in_numbers_id = i.jo_in_id
AFTER user EXPLAIN SELECT following is the output
In your EXPLAIN, I see that your joined tables ti and u are read with table-scans (type: ALL). This is probably the biggest problem for your performance.
You should make sure you have the following indexes created:
ALTER TABLE crew_tickets ADD KEY (sk_id, me_id, ti_expire);
ALTER TABLE unavailability ADD KEY (me_id, un_from, un_to);
That should help the joins to those tables work with index lookups instead of table-scans. I think they'll be accessed as covering indexes, too.
Also, please don't use the outdated "comma-joins." Especially do not mix both styles. It will bite you when you get surprised by the order of precedence between comma-joins and JOIN operators. See examples in Can someone help explain why not using a SQL JOIN is bad practice and wrong? or Error on JOIN mysql.
Write your joins this way:
FROM jo_2_no j2n
INNER JOIN jo_in_numbers i ON j2n.jo_in_numbers_id = i.jo_in_id
INNER JOIN jobs j ON j.jo_id = j2n.jo_id
LEFT JOIN venues_new v ON j.ve_id = v.ve_id
LEFT JOIN venues_meeting_place mp ON j.vmp_id = mp.vmp_id
LEFT JOIN venues_rooms r ON j.vr_id = r.vr_id
LEFT JOIN clients c ON j.cl_id = c.cl_id
LEFT JOIN members m ON j.me_id = m.me_id
LEFT JOIN skills sk ON j.sk_id = sk.sk_id
LEFT JOIN tasks ta ON j.ta_id = ta.ta_id
LEFT JOIN crew_tickets ti ON j.sk_id = ti.sk_id
AND j.me_id = ti.me_id AND j.jo_time_off < ti.ti_expire
LEFT JOIN unavailability u ON j.me_id = u.me_id
AND j.jo_time_on BETWEEN u.un_from AND u.un_to
I removed the redundant term in the join condition for u. The optimizer might eliminate that logic, but why make it work so hard?
I am trying to run the following query:
SELECT `aalv_test`.`aircraft`.*, `aalv_test`.`airports`.*, `aalv_test`.`bids`.*
FROM `bids`
LEFT JOIN `aalv_test`.`pilots` ON `bids`.`pid` = `pilots`.`id`
LEFT JOIN `aalv_test`.`schedules` ON `bids`.`fid` = `schedules`.`id`
LEFT JOIN `aalv_test`.`aircraft` ON `schedules`.`aircraft` = `aircraft`.`id`
LEFT JOIN `aalv_test`.`airports` AS `arr` ON `schedules`.`arricao` = `arr`.`icao`
LEFT JOIN `aalv_test`.`airports` AS `dep` ON `schedules`.`depicao` = `dep`.`icao`
WHERE `pilots`.`id` = 419
However,
MYSQL returns error #1051 - Table airports does not exist.
I don't know what the issue is and Google hasn't helped. Any ideas? Also, if I only use one alias, I only get one airport but I need both. And the data is only in the table airports which according to this query, does not exist. Also, if I try throwing an AS section in the SELECT clause, I get error 1064: syntax error near AS.
EDIT: Database name is aalv_test, the .* at the end specifies to use all fields in the table, and the middle part is the table name, yes I am chaining fields.
Try this:
SELECT a.*, arr.*, dep.*, b.*
FROM bids AS b
LEFT JOIN aalv_test.pilots AS p ON b.pid = p.id
LEFT JOIN aalv_test.schedules AS s ON b.fid = s.id
LEFT JOIN aalv_test.aircraft AS a ON s.aircraft = a.id
LEFT JOIN aalv_test.airports AS arr ON s.arricao = arr.icao
LEFT JOIN aalv_test.airports AS dep ON s.depicao = dep.icao
WHERE p.id = 419;
If someone could offer advice on improving the below query this would be most useful. I am unsure how I can make improvement when i have a left join twice in many instance to seperate tables. For example I have a location left join to the user table and a location left join to the image gallery table. I was unsure if i could optimise the sql from this point of view. It is very slow at the moment. I have ensured all columns are indexed on all joins and where statements.
SELECT im.alias_title, im.title,im.guid_id, im.description, im.hits, im.show_comment, im.can_print,
im.can_download, im.can_share, im.created_on, im.date_taken, im.approved, im.visible,
ad.address_line_1, ad.address_line_2, ad.town_village_city, ad.state_province_county, ad.postal_code, ad.other_address_detail, co.country,
geo.latitude, geo.longitude, geo.zoom, geo.yaw, geo.pitch,
c.make, c.model,
us.first_name, us.surname, uf.user_id, uf.real_name, uf.user_name, uf.gender, uf.description, uf.description, uf.buddy_icon_url, uf.first_taken_date, uf.first_date,
uf.time_zone_label, uf.time_zone_offset,
adf.address_line_1 as user_address_line_1, adf.address_line_2 as user_address_line_2, adf.town_village_city as user_town_village_city, adf.state_province_county as user_state_province_county,
adf.postal_code as user_postal_code, adf.other_address_detail as user_other_address_detail, cof.country as user_country,
geof.latitude as user_geolocation_latitude, geof.longitude as user_geolocation_longitude, geof.zoom as user_geolocation_zoom, geof.yaw as user_geolocation_yaw, geof.pitch as user_geolocation_pitch,
im.alias_title = in_image_alias_title AS image_selected -- image selected
FROM image im
LEFT JOIN address ad ON im.address_id = ad.id
LEFT JOIN country co ON ad.country_id = co.id
LEFT JOIN geolocation geo ON im.geolocation_id = geo.id
LEFT JOIN camera c ON im.camera_id = c.id
INNER JOIN user us ON im.user_id = us.id
LEFT JOIN user_flickr uf ON us.id = uf.id
LEFT JOIN address adf ON uf.address_id =adf.id
LEFT JOIN country cof ON ad.country_id = cof.id
LEFT JOIN geolocation geof ON uf.geolocation_id = geof.id
WHERE (im.alias_title = in_image_alias_title OR im.user_id = user_id)
AND im.approved = in_image_approved
AND im.visible = in_image_visible
AND (im.advertise_to <= NOW() OR im.advertise_to IS NULL)
ORDER BY image_selected DESC;
After the discussion / chat room, and learning more of what you were trying to do...
Build a compound index on the components associated with your where clause so all parts can be applied, not just the best of the first key element. Also, by removing the "alias_title" from the where clause (since you were getting the user ID based on the alias title to begin with), it was a redundant clause taking up more consideration in the query.
I would index on (user_id, approved, visible, advertise_to )
The results will come back and be small in the scheme of things, so your ultimate "order by" clause will have no problem with its final sort output.
I am trying to run that filters certain data from the result, including making sure that none of the records that have 'A_null_choice' as a value for it's moduleName.
$sql = "SELECT am.moduleID, m.moduleName, am.type, s.regNum, s.award
FROM tbl_awardModules am
LEFT OUTER JOIN tbl_module m ON am.moduleID = m.moduleID
INNER JOIN tbl_awardLevels al ON am.awardLevelID = al.awardLevelID
INNER JOIN tbl_award a ON al.awardID = a.awardID
INNER JOIN tbl_student s ON a.awardID = s.award
WHERE am.type <> 'C' AND regNum = '$student_id' AND m.moduleName NOT LIKE 'A_null_Choice'";
The rest of the query runs fine and there are no errors etc, it just keeps showing the records with that value.
Any insights will be greatly appreciated, thanks