can someone correct me on this syntax error. am trying to search users by firstname, lastname and their email
SELECT
USERID,
FNAME,
LNAME
FROM
USERS
WHERE
CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE('keyword ', ' ', '%'), '%', '%') NOT IN (
SELECT
PRODUCT_ID
FROM
ITEMS
WHERE
USER = 'JANE'
AND PRICE = '400'
AND ORDER = 'PAID'
)
i want to search and get userid,firstname and lastname. but i dont want to get jane, price = 400 and order = paid
TABLE USER
USERID FNAME LNAME EMAIL
1 JANE DEO TES#TED.COM
2 KEL DEO TES#TED.COM
3 MK DEO TES#TED.COM
TABLE ITEMS
PRODUCT_ID PRICE ITEM_NUM USER USERID
1 400 40 JANE 1
2 200 20 KEL 2
3 100 10 MK 3
so i want to get the rest people apart from JANE ROW
AND MY TRY IS
SELECT SND.USERID, SND.FNAME, SND.LNAME WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE('keyword', ' ', '%'), '%', '%') FROM items as M JOIN users as SND ON SND.USERID WHERE NOT EXISTS(SELECT * FROM ITEMS WHERE M.USER = 'JANE' AND M.PRICE ='400' AND M.ITEM_NUM ='40')
You cannot include a where clause between the select and the from clauses
SELECT
SND.USERID
, SND.FNAME
, SND.LNAME
>> no where clause here
FROM items AS M
INNER JOIN users AS SND ON SND.USERID
WHERE NOT EXISTS (
SELECT *
FROM ITEMS
WHERE M.USER = 'JANE'
AND M.PRICE = '400'
AND M.ITEM_NUM = '40'
)
Just include all the wanted conditions int the one (and only) where clause, combine those using AND's or OR's as needed. I have changed the join condition, and altered the NOT syntax used also.
SELECT
SND.USERID
, SND.FNAME
, SND.LNAME
FROM items AS M
INNER JOIN users AS SND ON M.userid = SND.userid
WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE('keyword', ' ', '%'), '%', '%')
AND NOT (
M.USER = 'JANE'
AND M.PRICE = '400'
AND M.ITEM_NUM = '40'
)
I'm not that comfortable with your use of concat() in the above however, it looks very inefficient, instead why not just repeat the LIKE tests on each column. (Yes it may make the SQL code longer, but that is not an indicator of the execution time of the query.)
SELECT
SND.USERID
, SND.FNAME
, SND.LNAME
FROM items AS M
INNER JOIN users AS SND ON M.userid = SND.userid
WHERE (
SND.FNAME LIKE CONCAT('%', REPLACE('keyword', ' ', '%'))
or SND.LNAME LIKE CONCAT('%', REPLACE('keyword', ' ', '%'))
or SND.EMAIL LIKE CONCAT('%', REPLACE('keyword', ' ', '%'))
)
AND NOT (
M.USER = 'JANE'
AND M.PRICE = '400'
AND M.ITEM_NUM = '40'
)
Note the use of indents and parentheses when using or in a where clause. (You do not have to place SQL into one row, not sure why you did that in the question.)
Related
I get the names twice and I need them to appear only once instead. How can I fix it?
I am using the SQL SELECT, stated below:
SELECT
( LTRIM(SUBSTRING(resource.name, CHARINDEX(',', resource.name) + 1, LENGTH(resource.name) - CHARINDEX(',', resource.name)))) AS firstname,
(SELECT GROUP_CONCAT(SUBSTRING(REPLACE(resource.name, '*Deleted*', '') FROM 1 FOR POSITION(',' IN REPLACE(resource.name, '*Deleted*', '' ))-1))) AS lastname,
(SELECT GROUP_CONCAT(obj SEPARATOR ', ') FROM rel_raci resource_raci_r WHERE resource_raci_r.PERSON_ID = resource.id AND resource_raci_r.RACI ='R' AND getOrgtype(obj_id) = 6 ) AS companycode,
(SELECT GROUP_CONCAT(REPLACE(obj, '*Deleted*', '')) FROM rel_raci resource_raci_r WHERE resource_raci_r.PERSON_ID = resource.id AND resource_raci_r.RACI ='R' AND getOrgtype(obj_id) = 4 ) AS organizationalunit,
(SELECT GROUP_CONCAT(obj SEPARATOR ', ') FROM rel_raci resource_raci_r WHERE resource_raci_r.RACI ='R' ) AS responsible,
resource.identifier AS identifier,
resource.phone AS phone,
organisation.keywords AS keywords,
resource.keywords AS persno,
resource.id AS obj_id,
resource.mobile AS mobile,
resource.e_mail AS email,
resource.alias AS alias,
resource.city AS city,
resource.postcode AS postcode,
resource.state AS state,
resource.street AS street,
resource.country AS country
FROM obj_resource resource
LEFT OUTER JOIN rel_raci resource_raci ON resource.ID = resource_raci.PERSON_ID
LEFT OUTER JOIN obj_resource organisation on organisation.ID = resource_raci.OBJ_ID
-- Gibt nur die markierten Massnahmen aus
WHERE CONTAINS($P{TE_SELECTIONS}, resource.id, -1)
Is there a way in SQL to set a default return value when NULL is returned for part of the results?
Here is my SQL:
SELECT p.id, p.title, concat( u1.meta_value, ' ', u2.meta_value ) as fullname, concat( r.name, ', ', c.name ) as location
FROM modules_profiles p
LEFT JOIN moonlight_usermeta u1 ON p.user_id = u1.user_id AND u1.meta_key = 'first_name'
LEFT JOIN moonlight_usermeta u2 ON p.user_id = u2.user_id AND u2.meta_key = 'last_name'
LEFT JOIN modules_regions r ON r.id = p.region_id
LEFT JOIN modules_countries c ON c.id = p.country_id
WHERE p.certification IN ( 'certified' ) AND p.country_id IN ( 2 )
ORDER BY p.user_id ASC
There are times when there is no region_id set for a given profile; therefore, NULL is returned for location for that respective user_id, even though we do have a country's name (c.name).
Is there a way in this case to just return the c.name only?
Use COALESCE() function like below, it will return the first non NULL value provided in list
COALESCE(col_name, 'default_value')
For your case, do
COALESCE(region_id, c.name)
I think, you are specifically talking about the part
concat( r.name, ', ', c.name ) as location
You can modify this using CASE expression as well
case when r.name is not null and c.name is not null
then concat( r.name, ', ', c.name ) else c.name end as location
You want to use MySQL's IFNULL(value, default) function.
Coalesce could help you
COALESCE(region_id, 'default value')
for my sense, I've a creative way of saving userdata. Let me explain:
I do not know which data is going to be saved in this database. For example, someone wants to save his icq number and i didn't know it before, where could he write it into? He dynamically creates a new field and in background there is an insert done in fields and an insert in user_fields where the new value of the new option is stored.
Table user:
id username
1 rauchmelder
Table fields:
id name
1 firstname
2 lastname
Table user_fields: (old values are stored as well as current, only youngest entry should be used)
id user_id fields_id value date
1 1 1 Chris 1.Mai
1 1 2 Rauch 1.Mai
1 1 1 Christopher 2.Mai
Result should be a View:
user.id user.username fields.firstname fields.lastname
1 rauchmelder Christopher Rauch
Firstly, does it make sense at all?
Secondly, should I solve it in MySQL or within the application?
Thridly, how to solve this in MySQL as a View?
In order to get the data into your columns, you can use an aggregate function with a CASE expression to convert the row data into columns.
If your fields are known ahead of time, then you can hard-code the values in your query:
select u.id,
u.username,
max(case when f.name = 'firstname' then uf.value end) firstname,
max(case when f.name = 'lastname' then uf.value end) lastname
from user u
left join
(
select uf1.*
from user_fields uf1
inner join
(
select max(date) maxDate, user_id, fields_id
from user_fields
group by user_id, fields_id
) uf2
on uf1.date = uf2.maxdate
and uf1.user_id = uf2.user_id
and uf1.fields_id = uf2.fields_id
) uf
on u.id = uf.user_id
left join fields f
on uf.fields_id = f.id
group by u.id, u.username;
See SQL Fiddle with Demo
But since you are going to have unknown fields, then you will need to use a prepared statement to generate dynamic SQL to execute. The syntax will be similar to this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN f.name = ''',
name,
''' THEN uf.value END) AS `',
name, '`'
)
) INTO #sql
FROM fields;
SET #sql
= CONCAT('SELECT u.id,
u.username, ', #sql, '
from user u
left join
(
select uf1.*
from user_fields uf1
inner join
(
select max(date) maxDate, user_id, fields_id
from user_fields
group by user_id, fields_id
) uf2
on uf1.date = uf2.maxdate
and uf1.user_id = uf2.user_id
and uf1.fields_id = uf2.fields_id
) uf
on u.id = uf.user_id
left join fields f
on uf.fields_id = f.id
group by u.id, u.username');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo
I need to get empolyees info from employees table, and their total wages from two different tables.
The SQL is approximately like this, but I don't really know how to use joins to do this:
CONCAT(first_name, ' ', last_name) from employees as e
Sum(hours*pay) where date is "THIS MONTH" and employee_id = e.id from taxed_work
Sum(hours*pay) where date is "THIS MONTH" and employee_id = e.id from nontaxed_work
I am not sure how to join these together properly. I don't want to see any of the employees that have not done either kind of work for the month, only those who have. I'm using mysql and will put the data in a table with php
If anyone could tell me how to do the "THIS MONTH" part that would be cool too. Just being lazy on that part, but figured while I was here...
Thanks for the help!
You could use correlated subqueries:
select concat(first_name, ' ', last_name)
, (
select sum(hours*pay)
from taxed_work tw
where tw.employee_id = e.id
and year(tw.date) = year(now())
and month(tw.date) = month(now())
)
, (
select sum(hours*pay)
from nontaxed_work ntw
where ntw.employee_id = e.id
and year(ntw.date) = year(now())
and month(ntw.date) = month(now())
)
from employees e
You can calculate their totals inside subquery.
SELECT a.id ,
CONCAT(first_name, ' ', last_name) FullName,
b.totalTax,
c.totalNonTax,
FROM employees a
LEFT JOIN
(
SELECT employee_id, Sum(hours*pay) totalTax
FROM taxed_work
WHERE DATE_FORMAT(`date`,'%c') = DATE_FORMAT(GETDATE(),'%c')
GROUP BY employee_id
) b ON b.employee_id = a.id
LEFT JOIN
(
SELECT employee_id, Sum(hours*pay) totalTax
FROM nontaxed_work
WHERE DATE_FORMAT(`date`,'%c') = DATE_FORMAT(GETDATE(),'%c')
GROUP BY employee_id
) c ON c.employee_id = a.id
Try this query.
select
CONCAT(first_name, ' ', last_name) as employee_name,
sum(case when t.this_date = 'this_month' then t.hours*t.pay else 0 end),
sum(case when n.this_date = 'this_month' then t.hours*t.pay else 0 end)
from employees e
left join taxed_work t on e.id = t.employee_id
left join nontaxed_work n on e.id = n.employee_id
group by (first_name, ' ', last_name)
Please replace the t.this_date and n.this_date fields with actual field names as I am not aware of the exact table structure. Also, replace the "this_month" value as per your need.
I'm trying to show the boroughs and postcodes a particular town in is.
My database is fairly well structured, with a table such as town, postcode and borough. There are also tables for each of the relationships town_postcode & town_borough.
Ideally I want the data returned as:
"Abbey Wood", "SE2", "Bexley, Greenwich"
"Barbican", "EC1, EC2", "City of London"
I've tried a few different approaches and I'm close but not there yet.
Any help would be appreciated... :)
So far I've tried
SELECT DISTINCT t.town,
GROUP_CONCAT( DISTINCT p.postcode SEPARATOR ', ' ) AS 'postcode',
GROUP_CONCAT( DISTINCT b.borough SEPARATOR ', ' ) AS 'borough'
FROM coverage_towns AS t,
coverage_boroughs AS b,
coverage_postcodes AS p,
coverage_towns_boroughs AS tb,
coverage_towns_postcodes AS tp
WHERE t.id = tp.town_id
AND p.id = tp.postcode_id
AND b.id = tb.borough_id
GROUP BY t.town
ORDER BY t.town ASC
Which returns
"Abbey Wood", "SE2", "Southwark, Hammersmith and Fulham, Tower Hamlets, Wandsworth, Enfield, Newham, LOTS MORE HERE"
"Barbican", "EC1, EC2", "Brent, Greenwich, Kensington and Chelsea, Westminster, Camden, LOTS MORE HERE"
I've also tried
SELECT DISTINCT t.town, (
SELECT SQL_CACHE DISTINCT GROUP_CONCAT( p1.postcode
SEPARATOR ', ' )
FROM coverage_postcodes AS p1
WHERE p1.id = tp.postcode_id
) AS 'postcode', (
SELECT SQL_CACHE DISTINCT GROUP_CONCAT( b1.borough
SEPARATOR ', ' )
FROM coverage_boroughs AS b1
WHERE b1.id = tb.borough_id
) AS 'borough'
FROM coverage_towns AS t, coverage_boroughs AS b, coverage_postcodes AS p, coverage_towns_boroughs AS tb, coverage_towns_postcodes AS tp
WHERE t.id = tp.town_id
AND p.id = tp.postcode_id
AND b.id = tb.borough_id
GROUP BY t.town
ORDER BY t.town ASC
Which returns
"Abbey Wood", "SE2", "Greenwich"
"Acton", "W3", "Greenwich"
"Aldersbrook", "E12", "Greenwich"
First query looks good, just add distinct inside the group_concat, like:
SELECT t.town
, GROUP_CONCAT(DISTINCT p.postcode SEPARATOR ', ' ) AS 'postcode'
, GROUP_CONCAT(DISTINCT b.borough SEPARATOR ', ' ) AS 'borough'
<more code here>
GROUP BY
t.town
SOLUTION
I came back to the question after a good coffee and the answer presented itself.
SELECT DISTINCT t.town,
GROUP_CONCAT( DISTINCT p.postcode SEPARATOR ', ' ) AS 'postcode',
GROUP_CONCAT( DISTINCT b.borough SEPARATOR ', ' ) AS 'borough'
FROM towns AS t, boroughs AS b, postcodes AS p, towns_boroughs AS tb, towns_postcodes AS tp
WHERE (t.id = tp.town_id AND t.id = tb.town_id)
AND (p.id = tp.postcode_id AND b.id = tb.borough_id)
GROUP BY t.town
ORDER BY t.town ASC