Is there a way in which I can change the way in which I see results of my query?
I'll use an example. I have a table with the id of a market, and then the name of the market, and the sport ID. I want the the sport id to be displayed as a sport name instead.
SELECT id, name, sport_id FROM markets where sport_id = 2;
I was thinking something like:
SELECT * FROM markets where sport_id = 2 as 'Football';
But that didn't work. I don't want to modify the results like an update would, I just want the results to be displayed as football instead of sport_id 2.
Is this possible?
if you do have only one table and like to give Alias then go for Query 1
SELECT id, name, sport_id AS 'sport name' FROM markets where sport_id = 2;
or
if you do have two different tables then Go for it
You need to join with other table as given below
SELECT m.id, m.name, t.sport_name
FROM markets m
JOIN other_Table t ON m.sport_id = t.sport_Id
where m.sport_id = 2;
I hope this might be helpful to solve your Issue.
You can do this with a JOIN to your sports table, something like:
SELECT m.id, m.name, s.sport_name
FROM markets m
JOIN sports s
ON m.sport_id = s.sport_Id
where m.sport_id = 2;
Related
I have two tables products and product_edits which hold product information on the pricelist. My app works in a way that if user changes any product info in products table it inserts it into product_edits table...
PRODUCTS table
pk|code|name |description|price|....
-----------------------------------
1 |QW1X|Product 1|...
2 |LW1X|Product 2|...
3 |DE1X|Product 3|...
PRODUCT_EDITS table
pk|product_id|code|name |description|price|....
-----------------------------------
1 | 2|LW1X|Product 2 new name|...
In above case I would like an SQL that returns records from both tables, but if product is found in product_edits table it selects only from product_edits and not also from products table.
I tried using standrd union but selects all records from both tables:
select code, name, description from products
union
select code, name, description from product_edits
It's better to use EXISTS instead of IN, in this case.
You want the search to stop once you found a match, not go over all of the results from product_edits.
So do it like this:
SELECT
code, name, description
FROM products p
WHERE NOT EXISTS (SELECT 1 FROM product_edits e WHERE e.code = p.code)
UNION
SELECT
code, name, description
FROM product_edits
select code, name, description from products
where code not in(select code from product_edits)
union
select code, name, description from product_edits
You may use IFNULL function
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull
If your tables are related, you can try something like this:
SELECT
p.code, IFNULL(pe.name, p.name), IFNULL(pe.description, p.description)
from
products p
left join product_edit pe on (p.id = pe.product_id)
UNION
SELECT
pe2.code, pe2.name, pe2.description
from
product_edits pe2
The first part will give you the products that are only in products table and the products that are in both tables, but with product_edits.description.
The second part will give you the products that are only in products_edits table, because union will remove repeated records
I am using MySQL and database server and I have two tables one is for customer and another for customer_contacts.
Here are the table structures:
customer(
id(pk, ai)
name
email
)
and
customer_contacts(
id(ai)
customer_id
first_name
last_name
)
Now my question is:
lets say I have one customer has many customer_contacts like this
customer
id name email
1 john john#example.com
and customer_contacts is like this (first row)
id customer_id first_name last_name
1 1 john doe
2 1 johnp pual
like this
So here I want to get all the contact details count for the id john. So can some one tell me how to get that?
Any help and suggestions will be really appreciable. Thanks
If the id is already known (as suggested by the question, it would be a plain
SELECT COUNT(*) from customer_contacts
WHERE customer_contacts.id = 1;
You just need to know the number of contactacs for the customer John, right?
SELECT COUNT(*)
FROM customer cus INNER JOIN customer_contacts con ON cus.id = con.customer_id
WHERE cus.name = 'john'
Nevertheless, It would be better if you know the id of John. Your query would be this:
SELECT COUNT(*)
FROM customer_contacts
WHERE customer_id = 1
Simply join both tables and use count() with group by c.id use where to filter records
select c.*,
count(*) total_contacts
from customer c
left join customer_contacts cc on(c.id = cc.customer_id)
group by c.id
Fiddle Demo
I'm sure this has been asked before but can't find the answer.
I have 3 tables OWNER, CAR, HOUSE
OWNER has 2 columns id and name
CAR has 3 columns id, ownerId and cartype
HOUSE has 4 columns id, ownerId, address, country
I want to write a SQL query that gets the owners name, cartypes, and addresses that are in Sweden
Here comes the tricky part. I want all the owners names and cartypes in the result table even if they don't own a house in Sweden. Can I get all that in 1 query or do I need to use 2? How would that query look?
You should be able to accomplish this with a simple left join:
SELECT O.name, C.cartype, H.address, H.country
FROM OWNER AS O
JOIN CAR AS C ON O.id = C.ownerid
LEFT JOIN HOUSE AS H ON O.id = H.ownerid AND Ucase(H.country) = "SWEDEN"
This will always give you a list of owners and their car types, in addition, it will give you a list of those that also happen to have a house address in sweden.
First you need to join the table then add new column in query by using CASE to check
SELECT o.* , c.* ,h.*,
(CASE WHEN h.county ='sweden' THEN h.county ELSE NULL END) AS HasCountry
FROM OWNER o
JOIN CAR c ON (c.ownerId =o.id)
JOIN HOUSE h ON (h.ownerId =o.id)
I have these tables :
TBL_PERSONS - a table for persons
TBL_SPORTS - a table for sports like football , basketball , tennis and ...
TBL_PERSON_SPORT - a table for any person's sports
for example Jo can play Football , and Basketball
I want to create a VIEW on persons and their sports.
something like this:
Jo|Football,Basketball
Jack|Football,Tennis
how should I write such VIEW?
thanks.
If you really want them to be a comma-separated list, that is the function of the GROUP_CONCAT() aggregate function. Substitute the correct names of your columns in place of name, person_id, sport_id, etc...
CREATE VIEW PERSON_SPORTS AS
(
SELECT
p.name,
GROUP_CONCAT(s.sport_name) AS sports
FROM
TBL_PERSONS p
LEFT JOIN TBL_PERSON_SPORT sp ON p.person_id = sp.person_id
JOIN TBL_SPORTS s ON sp.sport_id = s.sport_id
GROUP BY p.name
);
The above won't be very useful for joining against though, if you need to be able to separate out sports from the comma-separated list. Consider also just wrapping the ungrouped list in a view as:
CREATE VIEW PERSON_SPORTS AS
(
SELECT
DISTINCT
p.name,
s.sport_name AS sport
FROM
TBL_PERSONS p
LEFT JOIN TBL_PERSON_SPORT sp ON p.person_id = sp.person_id
JOIN TBL_SPORTS s ON sp.sport_id = s.sport_id
);
This will produce one row per sport, containing the person's name and sport name, where the person is duplicated as many times as needed for each sport.
I am trying to figure out a way to find shared credits by two people in a movie database, for example:
table: 'credits'
columns: 'id','movie','person'
My other problem is a person might have multiple credits for the same movie, so how do I filter out duplicates? Any help would be appreciated. The following is what I have so far. Am I on the right track?
SELECT DISTINCT movie
FROM credits
WHERE person = 'condition1' OR person = 'condition2'
GROUP BY movie
HAVING COUNT(*)=2
SELECT DISTINCT c1.movie
FROM credits AS c1
JOIN credits AS c2 on (c1.movie = c2.movie)
WHERE c1.person = 'john'
AND c2.person = 'kate'
This should work:
SELECT DISTINCT movie
FROM (
SELECT movie, COUNT(person) AS contributors
FROM credits
WHERE person IN('person1','person2')
GROUP BY movie, person
HAVING contributors>1
) t1
select distinct cr1.movie
from credits as cr1
join credits as cr2
using (movie)
where cr1.person = {person 1 goes here}
and cr2.person = {person 2 goes here}
You no need this HAVING COUNT(*)=2. It gives you only movies with two matches (one person with two credits or with both condition1 and condition2). More than that - you no need group by at all when you using distinct (in this case).
SELECT DISTINCT movie
FROM credits
WHERE person = 'condition1' OR person = 'condition2'