Fetch data from mapping table if the condition matches in multiple rows - php

product_id property_id
1 2
1 5
2 2
3 5
I have a mapping table as above. I want to get only product with id =1 if product_id in (2,5). i.e. I want to fetch data if the table contains both 2,5 not the data if it is with property_id only 2 or 5
select group_concat(distinct product_id) product_ids from table where property_id in (2,5)
UPDATE:
The property_id in can be property_id in(2,5,....). I get output from form as 2,5,.... and so on. Its not just for the single case. I just want the output if the condition in property_id in matches the whole series.

This how it could be done
select
product_id from
table_name
where property_id in (2,5)
group by product_id
having count(*) = 2
All you need to change having count(*) = 2 to the number of items inside IN() , right now its 2 and if you are looking at 3 property id then it will be 3 and so on.

select distinct a.product_id
from table a, table b
where a.product_id = b.product_id
and a.property_id = 2
and b.property_id = 5

Related

Order by a columns count where another column has a static value

I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;

Select from three tables

I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)

How do i update 1 field or 2 of a database table if another table has matching field values?

Let's say I have a table with these columns and values:
id field
-----------
1 0001
2 0002
3 0003
4 0004
5 0005
Then i have this other table with these fields and values:
id tid info_1 field_1 info_2 field_2
---------------------------------------------------
1 1 7 0000 null null
2 2 null null 10 0002
3 3 17 0003 null null
4 4 29 1111 null null
5 6 null null 44 0005
I need an update query that will update the records of table 2, just in the info columns, but only if table 1 has matching fields in table 2 and also only when id of first table and tid of second table match.
So for example table 1 id of 1 and table 2 tid of 1 match but field_1 or field_2 doesn't have the same field number as table 1.
Table 1 id of 2 and table 2 tid of 2 match and field_2 does have a matching number as the field in table one, so i would like to change info_2 of record 2 of table 2 to a number of my choosing. let's just say 50. in all cases the number in info_1 or info_2 will be changed to 50.
Now table 1 id of 3 and table 2 tid of 3 match and field_1 has matching number as the field in table 1, so i need to change info_1 to 50 for that 3rd record in table 2.
In record 4 the id and tid match but no fields match so skip that one.
In record 5 even though field_2 of table 2 matches field in table one, the id of first table and tid of second table do not match, so that 5th record can be skipped.
Here is what i tried so far:
$updatequery1 = "UPDATE table2 LEFT JOIN table1 a ON a.field = field_1 SET info_1 = 50";
$updateresult1 = mysql_query($updatequery1) or die(mysql_error());
So for this first attempt i didn't set id and tid to match, i just went right for looking for field matching. i also didn't incorporate field_2. not even sure if it can all be done in one pass or if multiple queries are needed. in any case, this attempt made all info_1 values equal to 50 even if there were no field matches.
Can someone help me with this query and make both info columns change at same time if possible?
If i were to do a select query for selecting all records that match in table 1 id and table 2 tid, it would be like this, but don't know how to incorporate it into the update query.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
I can also try and incorporate field matching and it would probably be something like this:
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
WHERE table1.field = table2.field_1
OR table1.field = table2.field_2
But again, wouldn't know how to turn that into an update query so that either info_1 or info_2 changes accordingly.
So to summarize in a quicker way. if id and tid match and field and field_1 match, info_1 should change to 50. if id and tid match and field and field_2 match, info_2 should change to 50
you can use CASE statement in the SET
UPDATE Table2 as T2
JOIN Table1 T1
ON T2.tid = T1.id
set
info_1 = CASE WHEN T2.field_1 = T1.field then 50 ELSE T2.field_1 END ,
info_2 = CASE WHEN T2.field_2 = T1.field then 50 ELSE T2.field_2 END

Selecting rows on Unique Pairs of columns from mysql Table

I have a Table table .
Now this has three columns table_id,content_id,content_type
What i want is to SELECT rows based on unique pairs of columns.
Say For example i have rows like this-->
id|content_id|content_type|
1 1 shirt
2 1 trouser
3 4 skirt
4 4 shirt
5 3 trouser
6 5 jeans
7 1 trouser
8 5 jeans
I want a query which selects Rows with id->1,2,3,4,5,6.
Rows with id->7,8 are not to be selected
Therefore it concludes that i dont want to select complete Duplicates of Rows
You can use a self join to pick a minimum row per group
select t.* from
test t
join (
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
) t1
on(t.id = t1.id
and t.content_id = t1.content_id
and t.content_type = t1.content_type)
Demo
or if there are only these 3 columns in your table then you can simply use min()
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
Demo
This is mysql-specific : If you use the GROUP BY function without using aggregate functions, the group by clause will behave as distinct, and pick up the first distinct row.
select id, content_id, content_type from test group by content_id, content_type order by id
Demo

problem with result order on distinct select query

i have a table like this
id name date group_id
1 n1 1 1
2 n2 1 1
3 n4 2 2
4 n5 2 2
i want ton write a query to return the group_id without duplicate ordered by date ASC
$query = " SELECT DISTINCT group_id FROM table ORDER BY date ASC";
this query will return 2 , 1 but this query is just going to consider date of the first row of each group_id to order the results
like if i have table like this
id name price date group_id
1 n1 2300 1 1
2 n2 3000 3 1
3 n4 4000 2 2
4 n5 2000 2 2
second row with with '1' as group_id has the biggest date so i should get 1,2 as result but query doesn't care about second row with the '1' group_id and still return 2,1
it only cares about the first row of each id for ordering the results
hopefully there is a easy way to solve this and i dont need to do something wird like putting everything in the 2d array and order that then deleting duplicates
Try this
select group_id,max(date) as SortDate
from table
group by group_id
order by SortDate
If I understand your problem, it sounds like you need to group by the date first.
SELECT group_id
FROM (
SELECT
group_id,
min(date) as min_date
FROM table
GROUP BY group_id
) as t
ORDER BY t.min_date;

Categories