I have a table customer, a table with different criterias (These criterias are then used to make ratings). Another table has the values and its keys in it.
table company
==============
int company_id
varchar name
bool status
table criteria
==============
int criteria_id
varchar name
bool status
table company_criteria
==============
int company_id
int criteria_id
int criteria_value
varchar comments
Now i display all the criterias in the form of select boxes which will have themselves a value against each criteria (already in the DB). Now i want the user to be able to search for different companies who have those specific criteria and the stored value.
e.g: table customer has a record with id 1
table criteria has records
1--->Reputation, 2--> Salary
table company_criteria has following records:
company_id | criteria_id | criteria_value |
============================================
1 1 10
1 2 20
Now the user sees two select boxes (remember there are two records in the criteria table) - with different options. He selects 10 from first select box and 20 from the second select box. How would i write the Query - I tried the following
SELECT DISTINCT `co`.*
FROM (`company` co)
JOIN `company_criteria` cc ON `co`.`company_id` = `cc`.`company_id`
WHERE (`cc`.`criteria_id`=1 AND `cc`.`criteria_value`>=10) AND (`cc`.`criteria_id`=2 AND `cc`.`criteria_value`>=20)
It just doesn't work - gives me no results - always. Appreciate any help - thanks.
Probably you want to put and OR instead of the AND here
SELECT DISTINCT `co`.*
FROM (`company` co)
JOIN `company_criteria` cc ON `co`.`company_id` = `cc`.`company_id`
WHERE (`cc`.`criteria_id`=1 AND `cc`.`criteria_value`>=10) OR (`cc`.`criteria_id`=2 AND `cc`.`criteria_value`>=20)
between the 2 where conditions....they both cant be true at the same time I think
Related
So im trying to get a table that shows 3 activities which have the id's of 6,7,8 and they must be from a specific country. It should show their names and the number of times they appear in the person_activity_interval but the table i get is only 1 row with the name of 1 activity and the count of all the activities that are listed. If i remove other 2 activities it gets me the correct name and count but i need a table with all 3. Do i need to use something like join or union?
SELECT name,count(activity_id)
FROM activity,person_activity_interval
WHERE oib IN (SELECT oib
FROM person
WHERE living_id IN (SELECT id
FROM living
WHERE country= "Poland"))
AND ((activity_id=8 AND id =8) OR (activity_id=7 AND id =7) OR (activity_id=6 AND id =6));
I'm trying to create the data for a form with some grouped checkboxes, which includes the user's previous selections. To prepare the data, I'm trying to return all the rows from my checkbox visibility table and add an extra column which indicates whether or not a row from the visibility table appears in the pivot table users_visibility. How do I do this?
Here is what I have. It returns what the two tables have in common based on a user's id.
select
visibility.id as visibility_id,
visibility.title as visibility_title,
users_visibility.users_id as checked
from visibility
left join users_visibility on users_visibility.visibility_id = visibility.id
where users_visibility.users_id = 2
Tables example
visibility
=======================
id title
-----------------------
1 Drivers licence
2 No Criminal record
3 Senior volunteer
users_visibility
===========================
id users_id visibility_id
---------------------------
1 2 3
What I would like returned
========================================
visibility_id visibility_title checked
----------------------------------------
1 Drivers licence NULL
2 No Criminal record NULL
3 Senior volunteer 2 (user_id or whatever indicator)
Thanks.
For a left join, conditions on all but the first table should be in the on clause.
Table aliases would also make the query easier to write and to read:
select v.id as visibility_id, v.title as visibility_title,
uv.users_id as checked
from visibility v left join
users_visibility uv
on uv.visibility_id = v.id and uv.users_id = 2;
I have the following situation. I have a table with all info of article. I will like to compare the same column with it self. because I have multiple type of article. Single product and Master product. the only way that I have to differences it, is by SKU. for example.
ID | SKU
1 | 11111
2 | 11112
3 | 11113
4 | 11113-5
5 | 11113-8
6 | 11114
7 | 11115
8 | 11115-1-W
9 | 11115-2
10 | 11116
I only want to list or / and count only the sku that are full unique. follow th example the sku that are unique and no have variant are (ID = 1, 2, 6 and 10) I will want to create a query where if 11113 are again on the column not cout it. so in total I will be 4 unique sku and not "6 (on total)". Please let me know. if this are possible.
Assuming the length of master SKUs are 5 characters, try this:
select a.*
from mytable a
left join mytable b on b.sku like concat(a.sku, '%')
where length(a.sku) = 5
and b.sku is null
This query joins master SKUs to child ones, but filters out successful joins - leaving only solitary master SKUs.
You can do this by grouping and counting the unique rows.
First, we will need to take your table and add a new column, MasterSKU. This will be the first five characters of the SKU column. Once we have the MasterSKU, we can then GROUP BY it. This will bundle together all of the rows having the same MasterSKU. Once we are grouping we get access to aggregate functions like COUNT(). We will use that function to count the number of rows for each MasterSKU. Then, we will filter out any rows that have a COUNT() over 1. That will leave you with only the unique rows remaining.
Take that unique list and LEFT JOIN it back into your original table to grab the IDs.
SELECT ID, A.MasterSKU
FROM (
SELECT
MasterSKU = SUBSTRING(SKU,1,5),
MasterSKUCount = COUNT(*)
FROM MyTable
GROUP BY SUBSTRING(SKU,1,5)
HAVING COUNT(*) = 1
) AS A
LEFT JOIN (
SELECT
ID,
MasterSKU = SUBSTRING(SKU,1,5)
FROM MyTable
) AS B
ON A.MasterSKU = B.MasterSKU
Now one thing I noticed from you example. The original SKU column really looks like three columns in one. We have multiple values being joined with hypens.
11115-1-W
There may be a reason for it, but most likely this violates first normal form and will make the database hard to query. It's part of the reason why such a complicated query is needed. If the SKU column really represents multiple things then we may want to consider breaking it out into MasterSKU, Version, and Color or whatever each hyphen represents.
I have two tables in my database. One is "TBL_USERS" and the other "TBL_RESPONDENTS".
TBL_USERS Columns
USR_NUM - Primary key, Integer(15)
NAME - Varchar(70)
TBL_RESPONDENTS Columns
RSPONDNT_NUM - Primary key, Integer(15)
SURVYR_NUM - Integer(15)
QN_NUMBR - Integer(15)
I am making a survey encoding application where one user is given a filled up interview form with a unique questionnaire number (QN_NUMBR). The value in the SURVYR_NUM column is the user who entered the form into the database (USR_NUM).
I have difficulty creating a query that will count how many forms that each user has entered into the database.
The output should be like this:
USER ID NAME QN FORMS ENETERED
1001 Mike 3
This is my inital query:
select tbl_users.name, tbl_users.usr_num, tbl_intrvw.qn_numbr from tbl_users inner join tbl_intrvw on tbl_users.usr_num = tbl_intrvw.survyr_num
and this is the output:
name usr_num qn_numbr
Mike 1001 2083
Mike 1001 8102
Mike 1001 1020
SELECT tbl_users.usr_num AS UserID, tbl_users.name AS UserName, COUNT(*) as QN_FORMS_ENETERED
FROM tbl_users INNER JOIN tbl_intrvw
ON tbl_users.usr_num = tbl_intrvw.survyr_num
GROUP BY tbl_users.usr_num,tbl_users.name
I will try to explain what I was asked to do to the best of my ability.
Let's say that we have developer access to DataBase A, which has many tables inside, but we are mostly concerned about two tables. The first one is called Accounts, and the second one Campaign. Now, inside the Campaign table we have many fields, but the most important are CampaignTypeID, and AccountID.
Inside the Accounts table the most important field are AccountID, and CustomerNumber. In the Accounts table we have many customers who have participated in different campaigns; therefore we can say that a single costumer can have many different CampaignTypeIDs under their account.
Now here is what I was asked to do: Show one CustomerNumber for each CampaignTypeID (5 types total). (I think that repeated CustomerNumbers are acceptable)
(Ex.)
CampaignTypeID CustomerNumber
1 34535
2 23525
3 23423
4 52355
5 23525
This is the query I used:
SELECT top 5 CustomerNumber[Customer Number], CampaignTypeID ,
FROM A.Account a
JOIN A.Campaign c ON a.AccountID = c.AccountID
WHERE CampaignTypeID IN (5)
GROUP BY CustomerNumber, CampaignTypeID
The result of this query would be something like:
(Ex.)
CampaignTypeID CustomerNumber
5 34535
5 23525
5 23423
5 52355
5 23525
Not exactly what I wanted, at first I plugged in all of the CampaignTypeIDs into the WHERE clause, but that would only return repeated CampaignTypeIDs.
(Ex.)
CampaignTypeID CustomerNumber
3 34535
3 23525
4 23423
5 52355
5 56678
3 23525
As you can see at this point my only option was to enter each CampaignTypeID, one by one. Then I would copy each one of those to a spread sheet. What I showed you above was just an example, but what I had to do had actually 40 different CampaignTypeIDs. It was a very tedious job that I know can be made A LOT more efficient.
If possible I would like to know a more efficient way to complete this task.
Thanks!
UPDATE: Alright, a small thing I should have added. The CampaignTypeID are not sequential, they are more like 5,6,7,8,9,10,50,65,110,250,1104,1114. Would this complicate thing?
SCHEMA
CREATE TABLE Accounts
(
AccountID int auto_increment primary key,
CustomerID int(20)NOT NULL
);
INSERT INTO Accounts
(CustomerID)
VALUES
(24),(22),(35),(256),(1246),(11),(224),(55),(664),(773),(234),(568),(245),(986),(768);
CREATE TABLE Campaign
(
CampaignID int auto_increment primary key,
CampaignTypeID int(20) NOT NULL,
CONSTRAINT fk_AccountID FOREIGN KEY(AccountID)
REFERENCES Accounts(AccountID)
);
INSERT INTO Campaign
(CampaignTypeID)
VALUES
(6),(7),(8),(9),(10),(245),(1140),(1150),(1160),(1170),(1180),(1190),(1240),(1250),(1260);
Select
CampaignTypeID,
Min(CustomerNumber)
From
A.Account a
Inner Join
A.Campaign c
On a.AccountID = c.AccountID
Where
CampaignTypeID Between 1 And 5
Group By
CampaignTypeID