Sql SELECT query just shows 1 row - mysql

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));

Related

Map users to a group using an id where the group description contain the id

What I am trying to achieve is:
Get the user group id from (tableB.value)
Map (tableB.value) to the (tableA.description) in the group.
Then list the (tableA.slug) for all those records that match.
I have two tables.
Table a contains the group description and slug. The description can be like "Hello I am 1" or "Hello I am 1,3" or "1"
Table b contains the user group id's Each user can have multiple group id's represented in the column value like "1,22,3"
I have been going around and around, the query below only seems to work for a one-to-one find. Where Table a matches exactly table b, I also have tried different like options but I keep crashing.
SELECT
a.description,
b.`value`,
a.slug
FROM
tableA as a
INNER JOIN
tableB as b
ON
FIND_IN_SET(a.`description`, b.`value`)
Table structures added upon request (sorry should have done the first)
TableA
id
slug
description
1
slug-1
Hello I am 1
2
slug-2
Hello I am 5,3
3
slug-3
Hello I am 4,22
TableB
id
value
1
1
2
22,3
3
3,1
Expected outcome TableC
id
value
slug
1
1
slug -1
2
22
slug-3
3
3
slug-2

Get records from multiple tables, but only show 1 per ID?

First of all im sorry for the title, it's difficult to explain what I'm trying to achieve.
I have 2 tables, a table for property records, and a table for the images uploaded for each property.
In my listing_details table I enter 1 record per property that has a unique ID and property slug. I have a prop_gallery table where I can have hundreds of records that share the same property slug so I can relate it back to my my property.
I'm trying to write a query to pull the records from both tables, but I only want to show each property once, at the moment it's looping through all the records in the gallery and showing that property for as many records their are in the gallery. Hope this makes sense?
My query is...
$listings = $db->query('
SELECT *
FROM listing_details
JOIN prop_gallery
ON prop_gallery.prop_gallery_id = listing_details.prop_slug
WHERE (prop_slug LIKE prop_gallery_id OR prop_gallery_id LIKE prop_slug)
AND listing_details.prop_mandate = 1'
)->fetchAll();
If there's a property called Liams house then there will be a record for that in listing_details and if I've uploaded 10 pictures, there will be 10 records for that in prop_gallery.
When I loop through my results this means I'm now showing Liams house 10 times, when I want to show it just the once.
EDIT
Result of the above query
prop_id prop_agent prop_title prop_slug prop_mandate id prop_gallery_id prop_gallery
37 2 House in switzerland house-in-switzerland 1 4 6 main1.png
37 2 House in switzerland house-in-switzerland 1 4 6 main2.png
37 2 House in switzerland house-in-switzerland 1 4 6 main3.png
You can use the ROW_NUMBER() function. Assuming you have a [any] property in the table listting_details you can sort rows by you can do it cleanly; I assumed the property recorded_at.
For example:
SELECT *
FROM (
SELECT *,
row_number() over(partition by prop_slug order by recorded_at) as rn
FROM listing_details d
JOIN prop_gallery g
ON g.prop_gallery_id = l.prop_slug
WHERE prop_slug LIKE prop_gallery_id OR prop_gallery_id LIKE prop_slug
AND d.prop_mandate = 1
) x
where rn = 1

Fetch rows either with one matching condition or all rows if no matching rows

I have a simple table
**targeted_url_redirect targeted_countries msg_type non_targeted_url**
http://new.botweet.com india,nepal,philippines NEW http://twisend.com
http://expapers.com United States,Canada OLD http://all.twisend.com
https://tweeasy.com india,england OLD http://all.twisend.com
I receive traffics on my website followerise.com and I want to redirect users to specific urls based on their countries as defined in the above table. I am able to write query to get rows for the users who coming from the countries that are target stored in my database. But I am looking for a solution to get all the rows if the targeted_countries condition not return any rows.
I written below queries.
SELECT * FROM tweeasy_website_redirection
WHERE message_type='OLD'
AND targeted_countries LIKE '%#country%'
This query gives me desired rows if visitor coming from the countries india,england,United States,Canada
But I want all rows (2nd and 3rd) should be fetched if a user coming from the countries not specified in targeted_countries column.
Also let me know if I need to restructure this table into 2 or more tables to get desired result.
One option uses not exists:
SELECT t.*
FROM tweeasy_website_redirection t
WHERE
t.message_type = 'OLD'
AND (
t.targeted_countries LIKE '%#country%'
OR NOT EXISTS (
SELECT 1
FROM tweeasy_website_redirection t1
WHERE t1.targeted_countries LIKE '%#country%'
)
)
When it comes to the structure of your table: one design flaw is to store list of values as CSV list. You should have two separate tables: one to store the urls, and the other to store the 1-N relationship between urls and countries. Something like:
table "url"
id targeted_url_redirect msg_type non_targeted_url
1 http://new.botweet.com NEW http://twisend.com
2 http://expapers.com OLD http://all.twisend.com
3 https://tweeasy.com OLD http://all.twisend.com
table "url_countries"
url_id country
1 india
1 nepal
1 philippines
2 united states
2 canada
3 india
3 england
select * from tweeasy_website_redirection
where targeted_countries not in (SELECT targeted_countries FROM stack WHERE targeted_countries LIKE '%#country%')

how to select specific rows in some condition

I tried to write a query that selects rows with steps that both user 1 and user 2 did, with combined number of times they did the step (i.e., if user 1 did step 1 3 times and user 2 did 1 time then the count should show 4 times.)
when I put condition as user_id=1, user_id=2 there is no error but it return nothing, when it should return some rows with values.
there is table step, and step taken
and table step has column id, title
table step_taken has column id, user_id(who performs steps), step_id
i want to find step that both of two user whose id 1,2 did
and also want to have the value as count added up how many times they performed that step.
for example if user id 1 did step named meditation 2 times,
and user id 2 did step named meditation 3 times,
the result i want to find should be like below ;
------------------------------
title | number_of_times
------------------------------
meditation| 5
------------------------------
here is my sql query
select title, count(step_taken.step_id)as number_of_times
from step join step_taken
on step.id = step_taken.step_id
where user_id = 1 and user_id=2
group by title;
it returns nothing, but it should return some rows of step both user1 and user 2 did.
when i wrote same thing only with user_id=1 or user_id=2, it shows selected information
how can I fix my code so it can show the information I want to get?
thanks in advance :)
user_id cannot be 1 and 2 at the same time. You need a second user table. Then join those on your criteria and count:
select title, count(u1.id) + count(u2.id) as number_of_times
from step u1 join step u2
on u1.id = u2.id
where u1.user_id = 1 and u2.user_id=2
group by title;
note: cannot tell what table title is in, or the purpose of step_taken was as step.id is identical.

Tricky multiple where condition MySQL Query

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