Hi I have a query which is working but I would like to remove certain values from the results.
At the moment I have WHERE (pafaddresses.CountryName !="England") but I would like this to be Where is not England and where is not Wales, I cannot get it to work for two != statements, I have tried Where !="England" or "Wales", without any success.
Any help would be appreciated
SELECT
visits.VisitPk,
visits.ClientFk,
visits.ClientSiteFk,
visits.AssessorFk,
visits.VisitStartDate,
visits.VisitEndDate,
visits.Duration,
visits.VisitStatus,
visits.TargetDate,
CONCAT(MONTHNAME(TargetDate), ' ', YEAR(TargetDate)) AS TargetMonth,
pafaddresses.PostCode,
visits.`Long`,
visits.Lat,
pafaddresses.id,
pafaddresses.CountryName,
CONCAT(Clients.ClientName, ', ', clientsites.SiteName, ', ', MONTHNAME(TargetDate)) AS Description
FROM visits
INNER JOIN clientsites ON visits.ClientSiteFk = clientsites.ClientSitePk
LEFT OUTER JOIN pafaddresses ON clientsites.ActualPAF = pafaddresses.id
INNER JOIN Clients ON visits.ClientFk = Clients.ClientPk
WHERE (pafaddresses.CountryName !="England")
ORDER BY visits.TargetDate, visits.VisitStartDate
You can do that either with
WHERE (pafaddresses.CountryName != 'England' AND pafaddresses.CountryName != 'Wales')
or
WHERE pafaddresses.CountryName NOT IN ('England', 'Wales')
Related
l'm working on a query for an ajax auto suggestion feature on my website and need a little help.
I'm using the following query:
SELECT
CONCAT(b.cat_name, ' > ', a.cat_name) as value,
a.cat_id as data
FROM categories a
LEFT JOIN categories b
ON b.cat_id = a.cat_parent_id
WHERE a.cat_path LIKE '%health%' OR a.cat_slug LIKE '%health%' OR a.cat_name LIKE '%health%' OR a.cat_legacy_path LIKE '%health%'
ORDER BY a.cat_path ASC LIMIT 500
The problem is coming up with root categories where parent_cat_id IS NULL
I'd still like them to appear in my results however as it stands now, the results are coming back empty for the root categories.
Can someone help me tweak this query so that l can accomplish what l'm looking to do?
If it makes any difference l'm doing this with CodeIgniter 3, so here's the actual active record code l'm using on the query:
$search_term = strtolower($search_term);
$this->db->select("CONCAT(b.cat_name, ' > ', a.cat_name) as value, a.cat_id as data");
$this->db->from('categories a');
$this->db->like('a.cat_path', $search_term, 'both', false);
$this->db->or_like('a.cat_slug', $search_term, 'both', false);
$this->db->or_like('a.cat_name', $search_term, 'both', false);
$this->db->or_like('a.cat_legacy_path', $search_term, 'both', false);
$this->db->join('categories b', 'b.cat_id = a.cat_parent_id', 'left');
$this->db->order_by("a.cat_path", 'ASC');
$this->db->limit('500');
$query = $this->db->get();
Ideally l'd like the root categories (where cat_parent_id IS NULL) to just be selected as a.cat_name as value as apposed to CONCAT(b.cat_name, ' > ', a.cat_name) as value.
Any help would be greatly appreciated.
You just need to make it conditional:
SELECT
COALECSE(CONCAT(b.cat_name, ' > ', a.cat_name), a.cat_name) as value,
or, checking more precisely for this exact cause of NULL,
SELECT
IF(a.cat_parent_id IS NULL, a.cat_name, CONCAT(b.cat_name, ' > ', a.cat_name)) as value,
So in your case in codeigniter that would translate to something like:
$this->db->select("COALECSE(CONCAT(b.cat_name, ' > ', a.cat_name), a.cat_name) as value, a.cat_id as data");
So I have query the output is supposed to show all medically trained staff and all volunteers that have first aid:
SELECT Concat (s.staff_fname, ' ', s.staff_lname) AS Staff_Name,
s.medically_trained,
s.staff_email_address,
Concat(v.volunteer_fname, ' ', v.volunteer_lname) AS Volunteer_Name,
v.first_aid,
v.volunteer_email_address
FROM volunteer v
JOIN staff s
ON v.volunteerid = s.staffid
WHERE s.medically_trained = ' yes'
AND v.first_aid = ' yes'
however when I execute my code it says empty set. What am I doing wrong?
I am a beginner coder so please dont judge if ive made an obvious mistake lol
THANK YOU!
First remove the space from 'yes' , it may also cause this issue.You may get the not null value in result.
If it is still blank then try to run the where clause individually on both of the tables,
Select *
FROM volunteer v where v.first_aid = 'yes';
and
select * from staff s
WHERE s.medically_trained = 'yes' ;
and check whether any one of it is giving you null as a result, if yes then your answer is correct.
Remove the spaces in where section:
SELECT Concat (s.staff_fname, ' ', s.staff_lname) AS Staff_Name,
s.medically_trained,
s.staff_email_address,
Concat(v.volunteer_fname, ' ', v.volunteer_lname) AS Volunteer_Name,
v.first_aid,
v.volunteer_email_address
FROM volunteer v
JOIN staff s
ON v.volunteerid = s.staffid
WHERE s.medically_trained = 'yes' /* <-- */
AND v.first_aid = 'yes' /* <-- */
Try this:
SELECT CONCAT(s.staff_fname,' ', s.staff_lname)AS Name,s.medically_trained AS Trained,s.staff_email_address AS Email_Address,'STAFF' cType
FROM staff s
WHERE s.medically_trained = ' yes'
UNION ALL
SELECT Concat(v.volunteer_fname, ' ', v.volunteer_lname) AS Name,v.first_aid AS Trained,v.volunteer_email_address AS Email_Address,'VOLUNTEER' cType
FROM volunteer v
WHERE v.first_aid = ' yes'
I used UNION ALL for the two table because I dont think Volunteer_ID will be Equivalent to STAFF_ID. I included cType Column for you to identify if the record was from Staff table or Volunteer
If you do a CONCAT('foo', 'null'), the result will be null. Check to see if that's not the case.
Also, it could be from either the JOIN clause or WHERE clause. Try running the query without JOIN and then without the WHERE clause and see if you'll still have empty sets.
My query I'am quite sure that the easiest way will be to remove some select columns - well its a filter grid query so no idea if worth, for sure add a LIMIT 1
and the count would be on the index id, what do you think about it? Just to make it a smooth not a long executing query.
$fquery = $db->query("SELECT zlec_status.nazwa AS Status,
piorytet.nazwa AS Priorytet,
Concat(koord.imie, ' ', koord.nazwisko) AS `Koordynator`,
Concat(zlec_adresy.miasto, ' - ', zlec_adresy.ulica, ' ',
zlec_adresy.oddzial)
AS `adres`,
zlec_z_dnia,zlec_id,
zlec_nr,
zlec_do,
zlec_ogran,
awizacje,
awizacja_na_dzien,
termin_zamkniecia,
tresc,
uwagi
FROM zlec
INNER JOIN koord
ON zlec.koord = koord.id
INNER JOIN zlec_adresy
ON zlec.zlec_addres = zlec_adresy.id
INNER JOIN piorytet
ON zlec.priorytet = piorytet.id
INNER JOIN zlec_status
ON zlec.status_zlecenia = zlec_status.id
HAVING adres LIKE concat('%',:az,'%'), array(
"az" => array_values($activeFilters)[0]
));
If anyone can help me rewrite my query to work in mysql 5 I would be very grateful. If anyone can provide links to solid, simple tutorials on how to rewrite old queries that would also be great.
My current (version 4) query looks like this:
SELECT
course.course_code,
course.course_title_sv AS course_title,
course.course_u_credits,
course.course_successive_level_scb_id,
s.successive_level_scb_order,
s.successive_level_scb_code,
LEFT (education_level.edu_level_name_sv, 1) AS course_edu_level, course.course_level,
GROUP_CONCAT(DISTINCT h.head_area_hv_title_sv SEPARATOR ', ') AS head_area_hv
FROM
course, course_event, course_event_package_links, package, education_level
LEFT JOIN
course_has_head_area_hv ON(course.course_id = course_has_head_area_hv.course_id)
LEFT JOIN
head_area_hv h ON(h.head_area_hv_id = course_has_head_area_hv.head_area_hv_id)
LEFT JOIN
successive_level_scb s ON(s.successive_level_scb_id = course.course_successive_level_scb_id)
WHERE
course.course_edu_level=education_level.edu_level_id AND
course.course_id=course_event.course_id AND
course_event.course_event_id=course_event_package_links.course_event_id AND
course_event_package_links.package_id=package.package_id AND
course.course_successive_level_scb_id != '' AND
package.package_id='6318'
GROUP BY course.course_id
Simply replace
SELECT ...
FROM course, course_event, course_event_package_links
...
WHERE course.course_id=course_event.course_id
AND course_event.course_event_id=course_event_package_links.course_event_id
by
SELECT ...
FROM course
JOIN course_event ON course_event.course_id = course.course_id
JOIN course_event_package_links
ON course_event_package_links.course_event_id = course_event.course_event_id
...
About your error message, are you sure course.course_id exists? Maybe it was replaced by course.course_code?
I created a VIEW using this code:
CREATE OR REPLACE VIEW aaa AS
SELECT pry.uid,
treg.nombre_es as region,
tpais.nombre_es as pais,
tdep.departamento,
dep_other, tciu.ciudad,
ciu_other
FROM tx_oriproyectos_proyectos AS pry
LEFT JOIN tx_oritablascomunes_regiones as treg ON pry.region = treg.uid
LEFT JOIN tx_oritablascomunes_paises as tpais ON pry.pais = tpais.uid
LEFT JOIN tx_oritablascomunes_departamentos as tdep ON pry.departamento = tdep.uid
LEFT JOIN tx_oritablascomunes_ciudades as tciu ON pry.ciudad = tciu.uid
And I obtained this and is Ok:
result http://finewebdesigns.com/images/mysql_view_result.jpg
And now I need to obtain concatenate results like this:
concatenated_field
---------------------------------------
Africa - ALbania - Tirana1 - Tirana2
Africa - Colombia - Guaviare - Calamar
How can I do that?
I tried this:
CREATE OR REPLACE VIEW aaa AS
SELECT CONCAT_WS (' - ', pry.uid, treg.nombre_es as region, tpais.nombre_es as pais, tdep.departamento, dep_other, tciu.ciudad, ciu_other)
FROM tx_oriproyectos_proyectos AS pry
LEFT JOIN tx_oritablascomunes_regiones as treg
ON pry.region=treg.uid
LEFT JOIN tx_oritablascomunes_paises as tpais
ON pry.pais=tpais.uid
LEFT JOIN tx_oritablascomunes_departamentos as tdep
ON pry.departamento=tdep.uid
LEFT JOIN tx_oritablascomunes_ciudades as tciu
ON pry.ciudad=tciu.uid
But I obtained:
#1583 - Incorrect parameters in the call to native function 'CONCAT_WS'
Ok, Thanks to #Mat I finally get this code, that is the expected solution to this problem.
CREATE OR REPLACE VIEW aaa AS
SELECT pry.uid, CONCAT_WS (' - ', treg.nombre_es, tpais.nombre_es, tdep.departamento, NULLIF(dep_other,''), tciu.ciudad, NULLIF(ciu_other,''))
FROM tx_oriproyectos_proyectos AS pry
LEFT JOIN tx_oritablascomunes_regiones as treg
ON pry.region=treg.uid
LEFT JOIN tx_oritablascomunes_paises as tpais
ON pry.pais=tpais.uid
LEFT JOIN tx_oritablascomunes_departamentos as tdep
ON pry.departamento=tdep.uid
LEFT JOIN tx_oritablascomunes_ciudades as tciu
ON pry.ciudad=tciu.uid
That obtains this: http://finewebdesigns.com/images/mysql_view_result_solved.jpg
You should be able to use the CONCAT_WS string function.
SELECT CONCAT_WS(' - ', treg.nombre_es, tpais.nombre_es, ...) FROM ...
From the docs:
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
so your null values will simply be ignored.
If you also want to skip empty strings, you can use throw the NULLIF function in the mix, as suggested by ypercube:
SELECT CONCAT_WS(' - ', NULLIF(col,''), ...) ...