PHP MySQL Joins - php

Is there a way I could somehow do the following?
Table ONE
id c_id type
-------------------
1 1 7
2 2 7
3 3 5
4 4 7
Table TWO
id title live
-------------------
1 row1 1
2 row2 0
3 row3 0
4 row4 1
The c_id column links the data from table ONE to table TWO. Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2".
I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1.
Here's how I thought I'd do it, but this doesn't seem to work:
SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5
I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1.

You were close... try using an implicit join:
SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5

select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 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;

Get dependent records from table

Here is the table structure. I want to fetch all the id_product which have value 2 and 601(here id_product = 5). If i use OR all the records will be populated which is not necessary.
id_product attribute_id Value
5 2 2
6 2 2
8 2 601
6 2 601
5 3 601
8 3 32
6 3 41
Any help would be appreciated. I don't want to use sub query :-p
You can use a group by query:
select
id_product
from
tablename
where
attribute_id=2 and values in (2,601)
group by
id_product
having
count(*)=2
this will select all products that have (attribute_id=2 and value=2) or (attribute_id=2 and value=601) in two different rows, and then it will count the number of rows returned and select only products that have two rows (one with value 2 and one with value 601).
Another option (it's not too clear from your question) is to use this where clause instead of the one on the query above:
where
(attribute_id=2 and value=2) or
(attribute_id=3 and value=601)
You can use this query in your case:
SELECT * FROM nameTable WHERE Values IN (2,601) and attribute_id = 2

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

How can I select all the values of a database and choose one of them by a percent

What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid

MySQL: Group By & Count Multiple Fields

If I have a single field, say, project_id where I want to count the occurrences, I would do something like:
select project_id, count(project_id) as count from tbl group by project_id, order by count desc
What if I want to count the occurrences of a combination of two fields in the same table, i.e. count the number of rows where the combination of project_id and service_id are unique?
So, so I have records in my table like:
project_id service_id
1 1
1 2
1 2
1 2
1 2
1 3
1 4
1 4
1 4
I would want the result of my query to be something like:
project_id service_id count
1 1 1
1 2 4
1 3 1
1 4 3
select project_id, service_id, count(*) as count
from tbl group by project_id, service_id
order by count(*) desc
Just add service_id to your group by and select list. That should do it.
EDIT -
As per comment from #Rajah it seems that for your expected output, you need to use
order by project_id asc, service_id asc

Categories