Query not resulting correct records - mysql

I have this query:
SELECT id, name FROM users
WHERE ((id = 1) OR (5 <= 5))
This should result all records as 5 = 5, but it's not. It's only resulting records where the id = 1.
What am I doing wrong?
EDIT:
This is the full query:
SELECT project_id, project_name, project_description, project_active,
users.user_firstname, users.user_lastname FROM projects
INNER JOIN users ON projects.user_id = users.user_id
WHERE (projects.user_id = 1 || 3 <= 3)
EDIT:
Found it =/
Something was wrong with the join; user_id didn't exist anymore for some reason.

Try:
EXPLAIN EXTENDED SELECT id, name FROM users where id=1 or 5<=5;
SHOW WARNINGS;
u will find mysql executes the query is:
SELECT id,name FROM users where 1
just means (id=1 or 5<=5) is same as 1

So do you want all records smaller or equal 5?
if so its only
SELECT id, name FROM users
WHERE id <= 5
Beside that
(5 <= 5)
is "5" a table row like id?

Do this
$q = ("SELECT id, name FROM users WHERE id = 1 || 5 <= 5");
This is how I do it.

Related

How to query not available value in the column?

I've a table as follows:
From this, I'm trying to get the user_id's where status IS NOT STATUS 4 AVAILABLE . In other word the results I'm expecting are:
User Ids: 17, 19 and 20.
I tried many queries but nothing didn't worked for me. For example I tried.
SELECT user_id WHERE status NOT IN (4)
The above above query still returns as follows:
User Ids: 1, 17, 18, 19 and 20.
I know the reason why it's returning but I'm not sure how to overcome the problem.
Any help would be appreciated.
You are going to need to do a subquery for this. For example:
Select user_id from table Where user_id not in
(select user_id from table where status = 4 and user_id is not null);
This allows you to exclude all user_id's that are in a status of 4.
Try this:
SELECT user_id
FROM mytable
GROUP BY user_id
HAVING COUNT(CASE WHEN status = 4 THEN 1 END) = 0
or, alternatively:
SELECT user_id
FROM mytable AS t1
WHERE NOT EXISTS (SELECT 1
FROM mytable AS t2
WHERE t1.user_id = t2.user_id AND status = 4)

MySQL select distinct and determine if only one user_id exists

I would like to run a query to show if there is one or more than one user_id for given poll_id.
Example 1: poll_id 1106 has only user_id 1 for all it's rows.
Example 2: poll_id 1106 has more than one user_id 1 for all it's rows.
Using the example given below, this php code works:
$sql = "
SELECT 1
FROM xf_poll_vote
WHERE poll_id='1106'
having count(distinct user_id) > 1";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection);
// format results by row
while ($row = mysql_fetch_array($sql_result)) {
$user_id[] = $row["user_id"];
}
$count = count($user_id);
echo $count;
Just a COUNT / DISTINCT I think.
SELECT poll_id, CASE WHEN COUNT(DISTINCT user_id) > 1 THEN 'Many' ELSE 'One' END AS ManyOrOne
FROM SomeTable
GROUP BY poll_id
select 1
from yourTable
where poll_id = #yourPoll_id
having count(distinct user_id) > 1
if returns you anything, that poll_id have more than one user. If not, only have one or zero users

Select Multiple Values From Single Column

I would like to select multiple values from a single column in a database table that equal to a number of values. I want all these values to match otherwise it should return no rows. I do not want to use "IN" as that is equal to "OR".
The following is a basic mockup of what it should do but it needs to be dynamic as I wish to use it with a PDO statement. If the database only contains id's 1 and 2 it should fail ie return no rows.
SELECT
id
FROM
reports
WHERE
id=1 AND id=2 AND id=3
I have the current code as follow which is incorrectly returning zero rows:
SELECT id,title
FROM reports
WHERE id IN (1,2)
GROUP BY title
HAVING COUNT(DISTINCT id) = 2
My current table structure is as follows:
http://www.sqlfiddle.com/#!2/ce4aa/1
You have to use HAVING COUNT(id) = 3 to ensure that the selected rows have all the three id's. Something like:
SELECT *
FROM reports
WHERE id = 1 OR id = 2 OR id = 3 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3;
Or:
SELECT *
FROM reports
WHERE SomeOtherField IN (SELECT SomeOtherField
FROM reports
WHERE id = 1 or id = 2 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3
);
Note that: You have to GROUP BY SomeOtherField where SomeOtherField is other field than id because if you GROUP BY id with HAVING COUNT(id) you won't get any records, since COUNT(id) will be always = 1.
Edit: fixed WHERE clause, OR's instead of AND's.
SQL Fiddle Demo

returning a new row for each record

in a previous question I asked how to return id's from 3 different tables if the user_id was matched to a record in each of these tables. the sql-
select
(select id from bla_facebook_accts where user_id = user.user_id) as facebook,
(select id from bla_linked_in_accts where user_id = user.user_id) as linkedin,
(select id from bla_twitter_accts where user_id = user.user_id) as twitter
from
(select 12 user_id) user
Works great, but I would like each the select statements that return a whether or not each of the tables has a record to return a new row, and return multiple rows if a table has more than one record for each .
Here you go. This will also return the source table of the information.
SELECT'facebook' source, id FROM bla_facebook_accts WHERE (user_id = 12)
UNION
SELECT 'linkedin' source, id FROM bla_linked_in_accts WHERE (user_id = 12)
UNION
SELECT'twitter' source, id FROM bla_twitter_accts WHERE (user_id = 12)
I believe you want to use a UNION construct:
select id from bla_facebook_accts where user_id = 12
UNION
select id from bla_linked_in_accts where user_id = 12
UNION
select id from bla_twitter_accts where user_id = 12

MySQL - Matching 2 rows (or more!) within a sub-query/table

I have this schema which I need to match 2 rows from
user_data : user_id, field_id, value
A sample output would be:
user_id field_id value
-----------------------------
1 1 Gandalf
1 2 Glamdring
How do I write a query which basically say "Find the user id of the user whose field_id 1 is Gandalf, and field_id 2 is Glamdring?"
SELECT FROM looks at one row at a time. I am stumped on this. I will also need to find a solution that scale gracefully (such as looking at three rows etc.)
You could run a query to get the users that match each of the conditions and intersect the results. Since MySQL doesn't support intersect you can do it with an n-way join:
SELECT T1.user_id
FROM Table1 T1
JOIN Table1 T2 ON T1.user_id = T2.user_id
WHERE T1.field_id = 1 AND T1.value = 'Gandalf'
AND T2.field_id = 2 AND T2.value = 'Glamdring'
I would try the following:
SELECT user_id
FROM user_data
WHERE ( field_id = 1 AND value= 'Gandalf' )
OR ( field_id = 3 AND value = 'Glamdring' )
GROUP BY user_id
HAVING COUNT( field_id ) = 2
It will search for all the rows that match one of your criteria, and use GROUP BY and HAVING afterwards to find the user_id that has the expected count of matches.
select * from user_date where ( field_id= 1 AND value='Gandalf' ) OR ( field_id =2 AND value ='Glamdring' ) ;
The HAVING clause is the key. It turns the query from an "OR" statement into an "AND" statement