i have recovered a database from a cmsms website and with Mysql i want to extract all the login information of the front-end users . i work with Heidisql
My problem user_proprerties DB is in this form:
Id UserID Type Data
1 2 email 1#2be.net
2 2 company lorme ipsum
3 2 fname testname
4 5 email& 2#2be.com
5 5 company empty
6 5 fname dolor sir amed
i want the data to be in this form:
Userid email company fname
2 1#2be.be lorem testname
5 2#2be.be emptydolor sir amed
this is the current mysql query that i have
select
cms_module_feusers_users.id,
cms_module_feusers_users.username,
cms_module_feusers_users.createdate,
cms_module_feusers_users.expires,
cms_module_feusers_properties.`data` email
from
cms_module_feusers_users
inner join cms_module_feusers_properties on cms_module_feusers_properties.userid = cms_module_feusers_users.id
where
cms_module_feusers_properties.title = 'email'
But now i'm stuck when i want the company name and fname.
(Fractionally) slower, but cleaner...
SELECT p.userid
, MAX(CASE WHEN type = 'email' THEN data END) email
, MAX(CASE WHEN type = 'company' THEN data END) company
, MAX(CASE WHEN type = 'fname' THEN data END) fname
FROM cms_module_feusers_properties p
GROUP
BY p.userid;
Add a join for each field you need
SELECT
user.id,
user.whatever,
<other stuff from cms_module_feusers_users...>
user_email.`data` as email,
user_company.`data` as company,
<other stuff from cms_module_feusers_properties...>
FROM cms_module_feusers_users user
LEFT JOIN cms_module_feusers_properties user_email ON user_email.userid = user.id AND user_email.title = 'email'
LEFT JOIN cms_module_feusers_properties user_company ON user_company.userid = user.id AND user_company.title = 'company'
<other joins from cms_module_feusers_properties...>
WHERE <conditions related to users...>
You need to use LEFT JOIN because some properties might be missing in cms_module_feusers_properties.
Also this assumes that each property appears at most once for each user.
Try this:
SELECT
cms_module_feusers_users.id,
cms_module_feusers_users.username,
cms_module_feusers_users.createdate,
cms_module_feusers_users.expires,
prop_data.data AS email,
prop_data_company.data AS company,
prop_data_fname.data AS fname
FROM
cms_module_feusers_users
INNER JOIN
(SELECT
*
FROM
cms_module_feusers_properties
WHERE
type = 'email') prop_data ON prop_data.userid = cms_module_feusers_users.id
INNER JOIN
(SELECT
*
FROM
cms_module_feusers_properties
WHERE
type = 'company') prop_data_company ON prop_data_company.userid = cms_module_feusers_users.id
INNER JOIN
(SELECT
*
FROM
cms_module_feusers_properties
WHERE
type = 'fname') prop_data_fname ON prop_data_fname.userid = cms_module_feusers_users.id
Related
I have the following output:
Username
shortname
data
test1
firm
Google
test1
type
IT
test1
agreed
1
test2
firm
Facebook
test2
type
IT
test2
agreed
1
test2
newsletter
1
I want to make another column that makes the following: if username has the shortname "newsletter" and "newsletter" has the data "1" then "yes", if username does not have newsletter then "no"
So I want to have the following output:
Username
yes/no
test1
no
test2
yes
I made the following code:
SELECT distinct(if(shortname = 'newsletter' and data = 1, 'Yes', 'No')) as 'Yes/no', username
FROM prefix_user as u
JOIN prefix_user_info_data as uid ON uid.userid = u.id
JOIN prefix_user_info_field as uif ON uid.fieldid = uif.id
But this writes out yes or no in every single row
You can use conditional aggregation as
SELECT username,
MAX(CASE
WHEN shortname = 'newsletter' AND data = 1 THEN
'Yes'
ELSE
'No'
END) AS "yes/no"
FROM prefix_user as u
JOIN prefix_user_info_data as uid
ON uid.userid = u.id
JOIN prefix_user_info_field as uif
ON uid.fieldid = uif.id
GROUP BY username
considering the alphabetical order of the word Yes comes later than No
Is this on Oracle? Actually even if not you may take the idea on below query.
select case
when shortname = 'newsletter' and data = '1'
then 'YES'
else 'NO'
end as yes_no
from prefix_user as u
JOIN prefix_user_info_data as uid
ON uid.userid = u.id
JOIN prefix_user_info_field as uif
ON uid.fieldid = uif.id
From the tags you've added, I'm taking it as MySQL. In your IF condition, in the case where shortname='newsletter', you will have to add a nested IF condition to check if the data value is equal to 1.
The query will be something like following for checking for all the users will be selected instead of DISTINCT based on the IF conditon:
SELECT
IF( shortname = 'newsletter',
IF (
data= 1,
'yes',
'No')
, 'No') AS 'Yes/no',
username,shortname, data
FROM
test1 AS u //Relace your table name here
//add joins with other tables that you requires.
I have three tables:
Users and companies can add feeds. The flag columns means
0 = user
1 = company
How can I perform this SQL query correctly? Thanks in advance for your help!
SELECT
feeds.feeds_id,
feeds.title
IF feeds.flag = 0
FROM user
INNER JOIN
feeds.user_or_company = user.user_id
ELSEif feeds.flag = 1
INNER JOIN
feeds.user_or_company = company.company_id
One way to solve this is to LEFT JOIN to both user and company tables and then select the appropriate name value based on feeds.flag:
SELECT f.feeds_id, f.title,
CASE f.flag WHEN 0 THEN u.name ELSE c.name END AS name
FROM feeds f
LEFT JOIN user u ON u.user_id = f.user_or_company
LEFT JOIN company c ON c.company_id = f.user_or_company
Output (for your sample data)
feeds_id title name
2 This title added twice by user User-1
3 This title added by company Company-2
1 This title added by user User-3
Demo on dbfiddle
I have a two tables one is Userregistration and second is user_verificationcode from which i have to get only those record whose email and mobile status are 1.Below are my table structure
Userregistration table
........................................
id fullname mobile_no email
.........................................
5 varun 12344567 abc#gmail
6 nitin 12345678 def#gmail
user_verificationcode
.............................................
id user_id codetype status
............................................
1 5 email 0
2 5 mobile 1
3 6 email 1
4 6 mobile 1
I want this kind of output
........................................
id fullname mobile_no email
.........................................
6 nitin 12345678 def#gmail
For this i have used below query but its not working i am not getting how to achieve this.
SELECT * FROM Userregistration
INNER JOIN user_verificationcode ON Userregistration.`id`=user_verificationcode.`user_id`
where user_verificationcode.`codetype`='email'
and user_verificationcode.`status`='1'
and user_verificationcode.`codetype`='mobile'
and user_verificationcode.`status`='1'
SELECT r.*
FROM Userregistration r
INNER JOIN user_verificationcode ve ON r.id = ve.user_id
and ve.codetype = 'email'
and ve.status = 1
INNER JOIN user_verificationcode vm ON r.id = vm.user_id
and vm.codetype = 'mobile'
and vm.status = 1
or
SELECT *
FROM Userregistration
where id in
(
select user_id
from user_verificationcode
group by user_id
having sum(codetype = 'email' and status = 1) > 0
and sum(codetype = 'mobile' and status = 1) > 0
)
You probably want to return only Userregistration fields, since you already know the info contained in user_verificationcode table. In this case you can use the following query:
SELECT t1.*
FROM Userregistration AS t1
JOIN (
SELECT user_id
FROM user_verificationcode
WHERE codetype IN ('mobile', 'email')
GROUP BY user_id
HAVING COUNT(DISTINCT codetype) = 2 AND SUM(status <> 1) = 0
) AS t2 ON t1.id = t2.user_id
You may would like to do something like this
SELECT Userregistration.id, Userregistration.fullName, Userregistration.mobile_no,
Userregistration.email FROM Userregistration
INNER JOIN user_verificationcode
ON Userregistration.id=user_verificationcode.user_id
WHERE user_verificationcode.codetype='email' AND user_verificationcode.status = 1
SELECT Userregistration.id,
Userregistration.fullName,
Userregistration.mobile_no,
Userregistration.email
FROM Userregistration
INNER JOIN user_verificationcode ON
Userregistration.id=user_verificationcode.user_id AND
user_verificationcode.status=1
WHERE user_verificationcode.codetype='email' AND
user_verificationcode.codetype='mobile'
G'day
I have 2 tables
RosterTable
ID: 1
Date: 19/02/14
UserA: N
UserB: N
UserC: Y
UserD: N
UserTable
ID: 1
Username: UserA
Name: John Smith
ID: 2
Username: UserB
Name: Joe Blogs
etc...
I'm trying to search the RosterTable to see who is rostered on indicated by the 'Y' and return the coulmn name, then use that column name to find the user details in the UserTable.
I can't change these tables as they a used for something else.
Thanks for any assistance
This is a very poor database design, if I understand the question correctly. Here is one way:
select *
from UserTable ut
where exists (select 1
from RosterTable rt
where rt.UserA = 'Y' and ut.username = 'UserA' or
rt.UserB = 'Y' and ut.username = 'UserB' or
rt.UserC = 'Y' and ut.username = 'UserC' or
rt.UserD = 'Y' and ut.username = 'UserD'
);
Here is a query. It should work. The main idea is to select the rows in RosterTable that has Y value for any of the 4 users and join UserTable with the associated id that is common in both table
select * from RosterTable rt
where rt.UserA = 'Y'
or rt.UserB = 'Y'
or rt.UserC = 'Y'
or rt.UserD = 'Y'
inner join UserTable ut on ut.ID = rt.ID
my entire query is a bit complex so I'll try to just show what you need to understand my problem, I'm fetching tickets from a data base, it displays the title, the creator, the people in charge of the ticket, and the last updater, for the creator and the people in charge I find their ID in tickets_users.users_id and their type 1 or 2 depending if they are the creator of the ticket or if they are in charge, for the title and all the informations of the ticket itself the table is named tickets now for the users their ID their name etc it's users
So my query looks like that:
SELECT
tickets.id,
tickets.name,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 1 THEN
CONCAT(users.firstname, ' ', users.realname)
END) AS creator,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 1 THEN
CONCAT(tickets_users.users_id)
END) AS creator_id,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 2 THEN
CONCAT(users.firstname, ' ', users.realname)
END) AS in_charge,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 2 THEN
CONCAT(tickets_users.users_id)
END) AS in_charge_id,
tickets.date,
tickets.priority,
tickets.date_mod,
tickets.status,
tickets.users_id_lastupdater,
MAX(CASE WHEN tickets.users_id_lastupdater = users.id
THEN CONCAT(users.firstname, ' ', users.realname)
END) AS last_updater,
tickets.content
FROM
tickets
JOIN tickets_users ON tickets_users.tickets_id = tickets.id
JOIN users ON users.id = tickets_users.users_id
WHERE tickets.id = ?");
My problem is there JOIN users ON users.id = tickets_users.users_id it works well for people in charge and creator, but if someone that is not in charge reply or modify the ticket he will not appear in "last modified by" because the last_updater ID is in the table tickets.
I tried to JOIN with JOIN users ON (users.id = tickets_users.users_id AND , glpi_tickets.users_id_lastupdater) but it didn't work. Any ideas?
ON users.id IN (tickets_users.users_id, tickets.last_updater)
And
You'd need to add the test AND users.id = tickets_users.users_id to
your CASE statements
Worked, thanks eggyal!