I've looked at a bunch of similar stackoverflow questions but I haven't been able to figure this out.
WHERE `Table1`.`Column1` = 'criteria1'
AND `Table1`.`Column2` = 'criteria2'
AND `Table1`.`Column3` LIKE 'criteria3'
AND IF(EXISTS IN `Table2`, (`Table2`.`Delete` <> 2, `Table2`.`SectionId` IS NOT NULL), '')
I'm trying to execute a query with five WHERE clauses, where the last two are based on the condition that the row exists in the joined Table2. If the condition is false, throw in an empty string ''.
I've tried variations of the above code but it's incorrect syntax. Is what I'm trying to do possible?
Can I use an IF inside a WHERE clause like that?
EDIT (here's the full query...):
$sql_query = "SELECT
`Courses`.`Id` AS CourseId,
IFNULL(`Courses`.`CourseName`, '') AS CourseName,
IF(`Courses`.`CourseNumber` > '', `Courses`.`CourseNumber`, -1) AS CourseNumber,
IFNULL(`Courses`.`CourseDepartment`, '') AS CourseDepartment,
IFNULL(`Courses`.`Notes`, '') AS CourseNotes,
IFNULL(`Courses`.`Year`, '') AS CourseYear,
IFNULL(`Courses`.`CourseType`, '') AS CourseType,
`Sections`.`Id` AS SectionId,
IFNULL(`Sections`.`SectionNumber`, '') AS SectionNumber,
IFNULL(`Sections`.`SectionName`, '') AS SectionName,
`StudentsCourses`.`CustomerId` AS CustomerId,
CONCAT(`Courses`.`Id`, '-', `Sections`.`Id`, '-', `StudentsCourses`.`FirstName`, `StudentsCourses`.`LastName`, '_', `StudentsCourses`.`TeacherEmail`) AS TempCustomerId,
IFNULL(`StudentsCourses`.`FirstName`, '') AS StudentFirstName,
IFNULL(`StudentsCourses`.`LastName`, '') AS StudentLastName,
IFNULL(`Customers`.`Email`, IFNULL(`StudentsCourses`.`StudentEmail`, '')) AS StudentEmail,
IFNULL(`StudentsCourses`.`TeacherFirstName`, '') AS TeacherFirstName,
IFNULL(`StudentsCourses`.`TeacherLastName`, '') AS TeacherLastName,
IF(`StudentsCourses`.`CustomerId` IS NOT NULL, `Customers`.`CustomerName`, CONCAT(`StudentsCourses`.`FirstName`, ' ', `StudentsCourses`.`LastName`)) AS FullName,
IF(`StudentsCourses`.`CustomerId` IS NOT NULL, CONCAT(REPLACE(`Customers`.`CustomerName`, ' ', ''), '_', `Customers`.`Email`), CONCAT(`StudentsCourses`.`FirstName`, `StudentsCourses`.`LastName`, '_', `StudentsCourses`.`TeacherEmail`)) AS NameKey,
`Customers`.`UserRoleId` AS UserRoleId,
`Customers`.`MagentoId` AS MagentoId,
`StudentsCourses`.`StudentId` AS StudentId,
`SiteProfile`.`StudentIdField` AS StudentIdField,
`SiteProfile`.`SchoolEmailDomain` AS SchoolEmailDomain,
`StudentsCourses`.`Id` AS StudentsCoursesId,
IFNULL(`Orders`.`Id`, '') AS OrderId
FROM `Courses`
LEFT JOIN `StudentsCourses` ON `Courses`.`Id` = `StudentsCourses`.`CourseId`
LEFT JOIN `BooksCourses` ON `Courses`.`Id` = `BooksCourses`.`CourseId`
LEFT JOIN `Products` ON `BooksCourses`.`ISBN` = `Products`.`ISBN`
LEFT JOIN `EbookVendors` ON `Products`.`EbookVendorId` = `EbookVendors`.`Id`
LEFT JOIN `Sections` ON `Sections`.`Id` = `StudentsCourses`.`SectionId`
LEFT JOIN `Customers` ON `StudentsCourses`.`CustomerId` = `Customers`.`Id`
LEFT JOIN `SiteProfile` ON `Courses`.`SchoolCode` = `SiteProfile`.`SchoolCode`
LEFT JOIN `Orders` ON `Customers`.`Id` = `Orders`.`CustomerId`
WHERE `Courses`.`SchoolCode` = '{$criteria["school_code"]}'
AND `Courses`.`Year` = {$criteria["year"]}
AND `Courses`.`CourseType` LIKE '{$criteria["term"]}'
AND `StudentsCourses`.`Delete` <> 2
AND `StudentsCourses`.`SectionId` IS NOT NULL";
It is not very clear exactly what you are asking, but I am guessing something like this condition might be what you are looking for:
AND (StudentCourses.CourseId IS NULL
OR (`StudentsCourses`.`Delete` <> 2
AND `StudentsCourses`.`SectionId` IS NOT NULL
)
)
Edit: It would best if you edited your question to explain in plain language what you want the condition to do, as it is not apparent at all what the intent was from the syntax you originally presented.
Related
I've been beating my head against the wall for a while now on this. I've tried following the thought behind Concat in If statement but I can't seem to figure out a way to make my specific need work. I'm now down to a syntax error in my CONCAT statement..'WHERE req.reqCreatedBy = '0' THEN 'Unknown' ELSE users.firstname,' ',users.lastn'. Could anybody give me some help on bringing the first and last name in on this query? I'm at a complete loss.
SELECT req.reqID as Id,
reqDesc.titleText as Title,
req.reqCity as City,
req.reqState as State,
req.areaID as Area,
area.areaname,
reqType.typeTitle as Type,
req.reqCreatedDate as Created,
req.reqEndDate as `End`,
CONCAT((CASE WHERE req.reqCreatedBy = '0' THEN 'Unknown' ELSE users.firstname,' ',users.lastname END))
AS Recruiter
FROM apps_job_request as req
INNER JOIN apps_job_request_description as reqDesc
ON req.reqTitle = reqDesc.titleID
INNER JOIN apps_job_request_type as reqType
ON reqDesc.typeID = reqType.typeID
INNER JOIN `assemble_users`.area AS area
ON area.areaid = req.areaID
INNER JOIN `assemble_users`.users AS users
ON users.username = req.reqCreatedBy
WHERE req.reqID is not null
AND req.reqActive = '1'
If check only one value req.reqCreatedBy, there may be simple IF statement, use CASE for multiple checked values.
SELECT req.reqID as Id,
reqDesc.titleText as Title,
req.reqCity as City,
req.reqState as State,
req.areaID as Area,
area.areaname,
reqType.typeTitle as Type,
req.reqCreatedDate as Created,
req.reqEndDate as `End`,
-- req.reqCreatedBy is '0' OR ''?
IF(req.reqCreatedBy = '0', 'Unknown', CONCAT(users.firstname, ' ', users.lastname)) AS Recruiter
FROM apps_job_request as req
INNER JOIN apps_job_request_description as reqDesc
ON req.reqTitle = reqDesc.titleID
INNER JOIN apps_job_request_type as reqType
ON reqDesc.typeID = reqType.typeID
INNER JOIN `assemble_users`.area AS area
ON area.areaid = req.areaID
INNER JOIN `assemble_users`.users AS users
ON users.username = req.reqCreatedBy
WHERE req.reqID is not null
AND req.reqActive = '1'
I'm trying to add row counts to an existing query but am getting an error
unknown column 'pl.GroupNumber' in 'on clause'
I'm apparently going to answer this too in case it helps someone else as, after writing all this here, it occurred to me to reverse the FROM to:
*FROM (SELECT #row_number:=0) AS t, parts_listing pl *
which seemed to do the trick! Don't quite understand why it worked but it did.
SELECT DISTINCT
(#row_number:=#row_number + 1) AS RowNum,
pl.ID,
pn.ID AS SubID,
IF(ListType,(SELECT MAX(ID) FROM parts_notes
WHERE PageNo=429
AND ListType IS NOT NULL AND SubPage IS NULL AND
(BasePart IS NOT NULL AND Models IS NOT NULL
AND (BasePart=pl.PartNo
AND pl.Models LIKE CONCAT('%', Models ,'%')))
OR (BasePart IS NOT NULL AND Models IS NULL
AND BasePart=pl.PartNo) ),NULL) AS SubMax,
ListType,
IndentText,
BaseGroup,
BaseName,
GroupName,
Title,
`Name`,
pl.Models,
pl.PartNo,
pn.PartNo AS SubPartNo,
pl.Quantity,
pn.Quantity AS SubQuantity,
pn.Description AS SubDescription,
ListType,
Column_1,
Column_2,
Column_3,
Column_4,
Column_5,
Column_6,
Column_7,
Column_8,
COALESCE(pl.GroupNumber, pn.GroupNo) AS GroupNo,
COALESCE(pl.Description, pn.Description) AS Description,
COALESCE(pl.PageNo, pn.PageNo) AS PageNo,
COALESCE(pl.SubPage, pn.SubPage) AS SubPage,
COALESCE(pl.RevDate, pn.RevDate) AS RevDate,
COALESCE(pl.Edition, pn.Edition) AS Edition
FROM parts_listing pl, (SELECT #row_number:=0) AS t
LEFT JOIN parts_notes pn ON pl.GroupNumber=pn.GroupNo
AND ((ListType < 5 AND (pl.PartNo=BasePart AND BasePart IS NOT NULL) OR BasePart IS NULL)
OR ((ListType > 4) AND (pl.GroupNumber=pn.GroupNo OR pl.PartNo=BasePart)))
WHERE BaseGroup=30 AND pl.PageNo=429 AND (pl.SubPage IS NULL
AND pn.SubPage IS NULL)
AND (pl.PageNo = pn.PageNo OR pn.PageNo IS NULL)
AND (pl.SubPage = pn.SubPage OR pn.SubPage IS NULL)
ORDER BY pl.ID, pn.ID
LIMIT 150;
Do not use commas in the FROM clause, even when you intend CROSS JOIN. The scoping rules are not what you expect, which is why you get an unknown column.
I put parameter conditions on the end, so I would write this as:
FROM parts_listing pl LEFT JOIN
parts_notes pn
ON pl.GroupNumber = pn.GroupNo AND
((ListType < 5 AND
(pl.PartNo = BasePart AND BasePart IS NOT NULL
) OR BasePart IS NULL
) OR
((ListType > 4) AND
(pl.GroupNumber = pn.GroupNo OR pl.PartNo = BasePart)
)
) CROSS JOIN
(SELECT #row_number := 0) params
I'm trying retrieve data using a LEFT JOIN and GROUP_CONCAT(). However, where values aren't present, I need something to represent the lack of data, or a method of identification for each, so that I can do something with them in the application.
CONCAT_WS(0x1D,
GROUP_CONCAT(DISTINCT bcod1.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod2.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod3.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod4.value
SEPARATOR 0x1F)) AS clients_options
Here, the four instances of GROUP_CONCAT() relate to either an INNER JOIN or a LEFT JOIN in a search performed by the user:
INNER JOIN
bookings_clients_options bco1 ON (bco1.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod1 ON (bco1.name = 'unit_code')
AND (bco1.bookings_client_option_id = bcod1.bookings_client_option_id)
AND (bcod1.value REGEXP ('.*'))
AND (bcod1.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco2 ON (bco2.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod2 ON (bco2.name = 'creditor_ev_number')
AND (bco2.bookings_client_option_id = bcod2.bookings_client_option_id)
AND (bcod2.value REGEXP ('.*'))
AND (bcod2.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco3 ON (bco3.client_id = '3')
LEFT JOIN
bookings_clients_options_data bcod3 ON (bco3.name = 'purchase_order_number')
AND (bco3.bookings_client_option_id = bcod3.bookings_client_option_id)
AND (bcod3.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco4 ON (bco4.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod4 ON (bco4.name = 'purchase_order_booking')
AND (bco4.bookings_client_option_id = bcod4.bookings_client_option_id)
AND (bcod4.value REGEXP ('Y'))
AND (bcod4.booking_attendee_id = bookings_attendees.booking_attendee_id)
Here, bcod3.value is where the results "collapse" in that if there's no value for bcod3.value but a value for bcod4.value, then bcod4.value drops into the space for bcod3.value.
As you can see, each of these columns has a name, but having tried...
GROUP_CONCAT(DISTINCT bcod" . $x . ".value SEPARATOR 0x1F) AS unit_code
... I got an error from the surrounding CONCAT_WS().
I tried...
IF(bcod" . $x . ".value = '', 'QQQ', GROUP_CONCAT(DISTINCT bcod" . $x . ".value SEPARATOR 0x1F))
... but that doesn't appear to do anything.
I tried...
GROUP_CONCAT(DISTINCT IF(bcod" . $x . ".value = 0, 'QQQ', bcod" . $x . ".value) SEPARATOR 0x1F)
.. and while it did grab the instances of bcod3.value, it also grabbed some of the instances of bcod1.value that have values which are not '0'.
I'm not 100% sure I understand your question, but I think I do. Sample data and desired results are worth 1000 words of explanation.
If the problem is that NULL or blank values are being ignored, then just do:
CONCAT_WS(0x1D,
coalesce(GROUP_CONCAT(DISTINCT bcod1.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod2.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod3.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod4.value SEPARATOR 0x1F), '')
) AS clients_options
It is generally considered a benefit that group_concat() and concat_ws() ignore NULL values. However, if you want to do something with them, just use coalesce().
I'm trying to pivotise my columns to get from rows to tables.
It's working like expected with the code I have now, but when my fields get updated I have to manually edit the query in order to update it.
I'm trying to automate the process but I'm not getting it to work. Any ideas?
This is the manual code which works:
SELECT
md.entity_guid AS guid, username, e.time_created, time_updated, e.enabled, banned,e.last_action, last_login,
MAX(IF(msn.string = 'question1', msv.string, NULL)) AS question1
FROM exp_metadata md
JOIN exp_metastrings msn ON md.name_id = msn.id
JOIN exp_metastrings msv ON md.value_id = msv.id
JOIN exp_users_entity u ON u.guid = md.entity_guid
JOIN exp_entities e ON e.guid = md.entity_guid
GROUP BY
guid
And this is the query I'm trying to do to automate it:
SET #sql = NULL;
SELECT
md.entity_guid AS guid, username, e.time_created, time_updated, e.enabled, banned, e.last_action, last_login,
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(msn.string = ''',
msn.string,
''', msv.string, NULL)) AS ',
msn.string
)
) INTO #sql
FROM exp_metadata md
JOIN exp_metastrings msn ON md.name_id = msn.id
JOIN exp_metastrings msv ON md.value_id = msv.id
JOIN exp_users_entity u ON u.guid = md.entity_guid
JOIN exp_entities e ON e.guid = md.entity_guid
SET #sql = CONCAT('SELECT e.guid, ', #sql, ' FROM exp_entities e GROUP BY e.guid');
I get an error in phpmyadmin about the last line.
When I delete the last line to see if it does anything, I get:
#1222 - The used SELECT statements have a different number of columns
Any help would be appreciated.
Thanks a lot, Dries
While trying to extract data from a view by joining it with two other tables, I'm getting the following error: "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."
And here is the query:
var expeditions = from VE in context.ViewExpeditions
join SIAGR in context.SiteInAdviseGoodsRef on VE.DeliveryNotes equals SIAGR.Value
join SIA in context.SiteInAdvise on SIAGR.SiteInAdviseId equals SIA.Id
where SIA.Id == SiteInAdviseID
select VE;
Here is the View ViewExpeditions:
SELECT ve.*, S.[$Refex] as SiteRefex,c.[$Refex] as ServiceRefex , DeliveryNotes = LEFT(o.list, LEN(o.list)-1),st.Status,st.StatusRefex
from(
SELECT
B.[$Id] AS Booking,
B.[$Refex] AS BookingRefex,
B.SiguxCPUE AS CartaDePorte,
SUM(ISNULL(BG.Weight, 0)) AS Weight,
SUM(ISNULL(BG.Volume, 0)) AS Volume,
ISNULL( SUBSTRING(B.Comments, 1, 100),'') AS Comments,
'Type' =
CASE
WHEN PickupSite IS NULL THEN 'Pickup'
ELSE 'Delivery'
END,
'Name' =
CASE
WHEN PickupSite IS NULL THEN PickupName
ELSE DeliveryName
END,
'City' =
CASE
WHEN PickupSite IS NULL THEN PickupCity
ELSE DeliveryCity
END,
'PostalCode' =
CASE
WHEN PickupSite IS NULL THEN PickupPostalCode
ELSE DeliveryPostalCode
END,
'ContactName' =
CASE
WHEN PickupSite IS NULL THEN ISNULL(B.PickupContactName,'')
ELSE ISNULL(B.DeliveryContactName,'')
END,
'ContactPhone' =
CASE
WHEN PickupSite IS NULL THEN ISNULL(B.PickupContactPhone,'')
ELSE ISNULL( B.DeliveryContactPhone,'')
END,
B.PickupDate AS DataExp,
B.DeliveryDate,
coalesce( B.PickupSite,B.DeliverySite) as 'Site',
b.Service,isnull(B.SiguxState,0) as SiguxState
FROM dbo.BookingGoods AS BG
INNER JOIN dbo.Booking AS B ON BG.BookingId = B.[$Id]
WHERE (B.ExecutedBy = 2) AND B.SiguxCPUE is not null AND B.[$IsDeleted]=0
GROUP BY B.PickupCity,b.PickupContactName,b.PickupContactPhone,b.PickupName,b.PickupPostalCode, B.DeliveryName,B.DeliverySite, B.DeliveryCity, B.DeliveryPostalCode, B.DeliveryContactName, B.DeliveryContactPhone, B.PickupDate, B.[$Id], B.SiguxCPUE, B.[$Refex], B.Comments,b.PickupSite,B.Service,B.SiguxState,B.DeliveryDate
) ve
INNER join [ViewBookingActualStatus] st on st.Booking=ve.Booking
INNER join Service c on ve.Service=c.[$Id]
INNER JOIN dbo.Site AS S ON S.[$Id] = ve.Site
outer APPLY
(
SELECT distinct
CONVERT(VARCHAR(12), dbo.BookingGoodsRef.Value) + ', ' AS [text()]
FROM
dbo.BookingGoodsRef
WHERE
dbo.BookingGoodsRef.BookingId = ve.Booking and BookingGoodsRef.Type=13
FOR XML PATH('')
) o (list)
Where am I going wrong about this?
NOTE: If I try to run the query in linqpad, it doesn't give me any errors at all, and generates the following sql:
SELECT [t0].[Booking], [t0].[BookingRefex], [t0].[CartaDePorte], [t0].[Weight], [t0].[Volume], [t0].[Comments], [t0].[Type], [t0].[Name], [t0].[City], [t0].[PostalCode], [t0].[ContactName], [t0].[ContactPhone], [t0].[DataExp], [t0].[DeliveryDate], [t0].[Site], [t0].[Service], [t0].[SiguxState], [t0].[SiteRefex], [t0].[ServiceRefex], [t0].[DeliveryNotes], [t0].[Status], [t0].[StatusRefex]
FROM [ViewExpeditions] AS [t0]
INNER JOIN [SiteInAdviseGoodsRef] AS [t1] ON [t0].[DeliveryNotes] = [t1].[Value]
INNER JOIN [SiteInAdvise] AS [t2] ON [t1].[SiteInAdviseId] = [t2].[$Id]
WHERE [t2].[$Id] = #p0
If you copy the generated SQL from LinqPad into SSMS and try to run it, do you get the results you expect?
I'd guess that the one of the columns on which you're joining (probably DeliveryNotes) is ntext or text. SQL Server can't join on text columns - you have to either cast both columns to varchar or use a substring on both columns.