I've got these tables:
table : _newapp_shop_products_events
+-------+-----------+-----------+-------------------+
| EventID | categoryID | Price |
+-------+-----------+-----------+-------------------+
| 9050 | 1 | 15.00 |
| 5048 | 2 | 12.00 |
+-------+-----------+-----------+-------------------+
table : _newapp_shop_products_categories
+-------+-----------+-----------+-------------------+
| CategoryID | Category_Name | photo |
+-------+-----------+-----------+-------------------+
| 1 | Tickets | one.jpg |
| 2 | Voucher | two.jpg |
+-------+-----------+-----------+-------------------+
table : _newapp_shop_products_activitys
+-------+-----------+-----------+-------------------+
| ActivityID | categoryID | Price |
+-------+-----------+-----------+-------------------+
| 80 | 1 | 13.00 |
| 95 | 2 | 18.00 |
+-------+-----------+-----------+-------------------+
And these are linked by a relationship, categories -> events, categories -> activities.
I want to display events and activities (the Events and Activity Tables not the _newapp ones) in one list and only display the items from both tables with categoryID = 1 for example. How would I solve this?
I have tried to join them or select them both in a union, it returns the rows fine but the internal link within mysql is broken. when I click on the second result set from 'Activitys' the id is correct and the price but when i click the id to go to the corresponding id it gives 0 rows result as it generates the query: SELECT * FROM representin_nl_-_1.Events WHERE Id = 5 But that is the wrong table, it should search it in SELECT * FROM representin_nl_-_1._newapp_shop_products_activitys WHERE Id = 5 instead. Result: The first 9050 when i click it, it goes to the correct row but the ids after that are not found. Result:
You probably want a UNION
SELECT 'Event' AS Type, EventID AS Id, Price
FROM Events
WHERE CategoryId = 1
UNION ALL
SELECT 'Activity' AS Type, ActivityID AS Id, Price
FROM Activity
WHERE CategoryId = 1
What do you mean Id conflict? You could do something like this instead
SELECT EventID, null AS ActivityId, Price
FROM Events
WHERE CategoryId = 1
UNION ALL
SELECT null, ActivityID, Price
FROM Activity
WHERE CategoryId = 1
Related
I have the following table:
+---------+--------------+----------+
| item_id | location_id | price |
+---------+--------------+----------+
| 1 | 1 | 100 |
| 1 | 1 | 250 |
| 1 | 2 | 50 |
| 2 | 1 | 250 |
| 2 | 1 | 1000 |
| 3 | 1 | 1000 |
| 3 | 2 | 100 |
+---------+--------------+----------+
I can reduce this down to the minimum values using this query
SELECT
item_id, location_id, MIN(price) AS Price
from
table
GROUP BY item_id , location_id
This gets me
+---------+--------------+----------+
| item_id | location_id | price |
+---------+--------------+----------+
| 1 | 1 | 100 |
| 1 | 2 | 50 |
| 2 | 1 | 250 |
| 3 | 1 | 1000 |
| 3 | 2 | 100 |
+---------+--------------+----------+
I want to reduce this further. I am using the rows with a location_id of 1 as a reference row. For each row that has an item_id matching the reference row's item_id but a different location id. I want to compare that row's price with the reference row's price. If the price is lower than the reference row's price, I want to filter that row out.
My final result should include the reference row for each item id and any rows that met the criteria of the price being lower than the reference row price.
I have a hunch that I can use the HAVING clause to do this but I am having trouble compiling the statement. How should I construct the HAVING statement?
Thanks in advance
Nah, having can't help you like this, having is for things like you need filter min() result for something
e.g:
select id,min(price) from table where date = '2016-3-18' group by id having min(price) = 50
it will show you the records that min(price)=50
let's back to your case, there are lots of way to do that,
1. left join
select a.item_id,a.location_id,a.price
from table a
left join table b
on a.location_id = b.location_id and a.price > b.price
where b.price is null
2. exists
select a.item_id,a.location_id,a.price
from table a
where exists(
select 1 from
(select location_id,min(price)as price from table group by location_id)b
where a.location_id = b.location_id and a.price = b.price
)
normally i ll recommand you use exists
I've got two tables, in which both id_health_center and id_user are used as composite key:
health_center_review
id_health_center
id_user
review
health_center_grade
id_health_center
id_user
grade_service
I would like to build a query that would return reviews from health_center_review with grades given by the same user. For example:
id_health_center | id_user | review
---------------------------------------
1 | 1 | something 1
1 | 2 | something 2
id_health_center | id_user | grade_service
-------------------------------------------
1 | 1 | 5
would return:
id_user | review | grade_service
----------------------------------------------------
1 | something 1 | 5
2 | something 2 | NULL
I have tried the following query:
SELECT hcr.id_user, review, grade_service
FROM health_center_reviews hcr
LEFT JOIN health_center_grades hcg ON hcg.id_health_center = hcr.id_health_center AND hcg.id_user = hcr.id_user
WHERE hcr.id_health_center = 1;
hovewer that only returns reviews if they have grades. If I omit the user part in join clause, it returns the grade for other user.
I have a database that store transaction logs, I would like to count all the logs for that day and group them based on prod_id
MySQL table structure:
Table name = products
+------+---------+------------+--------+
| ID | PROD_ID | DATE | PERSON |
+------+---------+------------+--------+
| 1 | 2 | 1400137633 | 1 |
| 2 | 2 | 1400137666 | 1 |
| 3 | 3 | 1400137125 | 2 |
| 4 | 4 | 1400137563 | 1 |
| 5 | 2 | 1400137425 | 2 |
| 6 | 3 | 1400137336 | 1 |
+------+---------+------------+--------+
MYSQL CODE:
$q = 'SELECT count(ID) as count
FROM PRODUCTS
WHERE PERSON ='.$db->qstr($person).'
AND DATE(FROM_UNIXTIME(DATE)) = DATE(NOW())';
so what I get is the number of items for the given date. Since the date is the same as all other entries. however I would like to group the items by prod_id, I tried GROUP BY PROD_ID but that did not give me what I want. I would like it to group if the PROD_ID is multiple and the date is the same display as one entry while still count the others
so here I should get an output ($Person = 1).... 2+2+2=1 +3 +4 so total should be 3
any suggestions?
Use DISTINCT with COUNT on PROD_ID.
Example:
SELECT count( distinct PROD_ID ) as count
FROM PRODUCTS
WHERE PERSON = 1 -- <---- change this with relevant variable
AND DATE( FROM_UNIXTIME (DATE ) ) = curdate();
And I suggest you to use Prepared Statement to bind values.
Hello there I have a following table
------------------------------------------
| id | language | parentid | no_daughter |
------------------------------------------
| 1 | 1 | 0 | 2 |
------------------------------------------
| 1 | 1 | 0 | 2 |
------------------------------------------
| 2 | 1 | 1 | 1 |
------------------------------------------
| 2 | 2 | 1 | 1 |
------------------------------------------
| 3 | 1 | 1 | 0 |
------------------------------------------
| 3 | 2 | 1 | 0 |
------------------------------------------
| 4 | 1 | 2 | 0 |
------------------------------------------
| 4 | 2 | 2 | 0 |
------------------------------------------
| 5 | 1 | 2 | 0 |
------------------------------------------
| 5 | 2 | 2 | 1 |
-----------------------------------------
| 5 | 1 | 4 | 1 |
------------------------------------------
| 5 | 2 | 4 | 1 |
------------------------------------------
Scenario
Every record has more than one rows in table with different language ids. parentid tells who is the parent of this record. no_daughter columns tells against each record that how many child one record has. Means in Ideal scenario If no_daughter has value 2 of id = 1 , it means 1 should be parentid of 2 records in same table. But If a record has more than one exitance with respect to language, it will be considered as one record.
My Problem
I need to find out those records where no_daughter value is not correct. It means if no_daughter is 2, there must be two records whoes parentid has that id. In above case record with id = 1 is valid. But record having id = 2 is not valid because the no_daughter = 1 but actual daughter of this record is 2. Same is the case with id=4
Can any body tell me how can I find these faulty records?
Updated after answers
Ken Clark has and shola has given answer which return same result for example shola query is
SELECT DISTINCT
id
FROM
tbl_info t
INNER JOIN
(SELECT
parentid,
COUNT(DISTINCT id) AS childs
FROM
tbl_info
GROUP BY parentid) AS parentchildrelation
ON t.id = parentchildrelation.parentid
AND t.no_daughters != parentchildrelation.childs
This query is returning those ids who have been used as parentid somewhere in table but having wrong no_daughter values. But not returning ids that has value in no_daugter columns but have not been used as parentid any where in table. For exampl id = 5 has no_daughter = 1 but it is not used as parentid in table. So it is also a faulty record. But above query is not capturing such records.
Any help will be much appreciated.
Try this:
SELECT DISTINCT
id
FROM
tbl_info t
Left JOIN
(SELECT
parentid,
COUNT(DISTINCT id) AS childs
FROM
tbl_info
GROUP BY parentid) AS parentchildrelation
ON t.id = parentchildrelation.parentid
Where t.no_daughters != parentchildrelation.childs
Try this:
SELECT id FROM tinfo t inner join
(SELECT parentid, COUNT(distinct language ) as childs FROM tinfo group by parentid) as summary
on t.id=summary.parentid and t.no_daughters!= summary.childs
try this
Select Distinct * From tablename t
Left Join
(
Select COUNT(t1.Id) Doughter,t1.parentid,t1.language From tablename t1 Group By t1.parentid,t1.language
)tbl
On t.id=tbl.parentid And tbl.language=t.language And t.no_daughter<>tbl.Doughter
ok so table looks like this
owner_id | creator_id | receiver_id | content | created
1 | 1 | 2 | hoho | 2011-27-05
2 | 1 | 2 | hoho | 2011-27-05
1 | 2 | 1 | eoeo | 2011-28-05
2 | 2 | 1 | eoeo | 2011-28-05
1 | 1 | 3 | aaaa | 2011-29-05
3 | 1 | 3 | aaaa | 2011-29-05
2 | 2 | 3 | bbbb | 2011-30-05
3 | 2 | 3 | bbbb | 2011-30-05
what I would like is to select only the last record for owner_id = 1 from each creator_id OR receiver_id and it doesn't matter is it last from creator_id or receiver_id as long it is last record where unique creator_id or receiver_id is.
do the result should look like this
owner_id | creator_id | receiver_id | content | created
1 | 1 | 3 | aaaa | 2011-29-05
1 | 2 | 1 | eoeo | 2011-28-05
Start by fetching the last date per owner_id, creator_id:
select owner_id,
creator_id as user_id,
max(created) as max_created
from data
where owner_id = 1
group by owner_id, creator_id
And the same per owner_id, receiver_id:
select owner_id,
receiver_id as user_id,
max(created) as max_created
from data
where owner_id = 1
group by owner_id, receiver_id
Then union and apply the max another time:
select owner_id, user_id, max(max_created) as max_created
from (
[the first of the above]
union all
[the second of the above]
) as users
group by owner_id, user_id
Then join it in a subquery:
select data.*
from data
join ([the union query above]) as max_dates
on max_dates.owner_id = data.owner_id
and max_dates.user_id in (data.creator_id, data.reporter_id)
and max_dates.max_created = data.created
where owner_id = 1 -- avoids scanning the whole table
That'll yield the rows where creator_id appeared last, and those where reporter_id appeared last.
You cannot reduce it further without dropping valid rows. Suppose the returned (creator/reporter) pairs are (1,2) and (2,1). Then both rows would be eliminated if you sought the absolute last appearance of each user.