Multiple check in same column in Mysql - mysql

I have a table as below:
Here, I wanna run a query which looks like this:
SELECT model_id
FROM model_attributes
WHERE ((attributes_id=2 and attributes_value='32mb')) AND ((attributes_id=4 AND attributes_value='5.00 inch') OR (attributes_id=4 and attributes_value='6.00 inch')) AND ((attributes_id=5 and attributes_value='here') OR (attributes_id=5 and attributes_value='asfdsdf'))
which returns model_ids 8, 9. But it seems like I can't put multiple conditions in the same fields.
I also tried solutions in this post still nothing ! How can I get this result ? The way I used the AND clause is incorrect but resembles what logic I wanna implement in the query.

You are asking for rows where attributes_id is 2 and 4 and 5, that will never be true.
Try this:
WHERE (attributes_id=2 AND attributes_value='32mb') OR
(attributes_id=4 AND attributes_value in ('5.00 inch', '6.00 inch') OR
(attributes_id=5 AND attributes_valuein ('here', 'asfdsdf')

SELECT model_id
FROM model_attributes
WHERE ((attributes_id=2 and attributes_value='32mb')) or ((attributes_id=4 AND attributes_value='5.00 inch') OR (attributes_id=4 and attributes_value='6.00 inch')) or ((attributes_id=5 and attributes_value='here') OR (attributes_id=5 and attributes_value='asfdsdf'))
make and to orlike above i think and was logically wrong

Related

How can I use OR operator with 2 tables?

I'm trying to making a search option for my website project. I have to search 2 columns from 2 tables. After that, I'll write that query in my php code. Then it will list all the data about it. But it seems like I'm doing it wrong. What should I do?
select *
from mudurler,subeler,veriler
where mudurler.sube_id=subeler.sube_id
and veriler.sube_id=subeler.sube_id
and subeler.sube_ad like "%this%" or mudurler.adSoyad like "%that%"
When I go, if there is a valid value on sube_ad it works perfectly. But when i try to put valid value on adSoyad MySQL turns an empty result no matter what the value is.
You would have no problem if you used proper, explicit, standard JOIN syntax:
select *
from mudurler m join
subeler s
on m.sube_id = s.sube_id join
veriler v
on v.sube_id = s.sube_id
where s.sube_ad like '%this%' or
m.adSoyad like '%that%';
May be you should try:
and (subeler.sube_ad like "%this%" or mudurler.adSoyad like "%that%")

Laravel 5.6 - GroupBy is not working

I have tried it in several ways but it doesn't work (seems like ignoring it). So what I tried:
$user->notes()->groupBy('title')->get();
Above way completely ignores groupBy and just returns collection of notes.
Note::where('user_id', $user->id)->groupBy('title')->get();
Exactly same output with this one too.
In my database.php, the database is set to 'strict' => false
I have also tried using raw db query, it returns it in a weird format (returns 1 row for each title when I use groupBy)
DB::table('notes')->where('user_id', $user->id)->groupBy('title')->get();
I have seen many people facing this issue however none of the suggested ways (above) solved the issue.
I can achieve what I want with using collection->each(function ($note) {...} ), however while there is groupBy to make achieve this easily with 1 line, why the heavy work..
Does anyone has any idea why it doesn't work?
you just need to call first the ->get() then the ->groupBy() method.
Thats because in a SQL Query you need to select first the elements, then group.
So your code need to be like:
DB::table('notes')->where('user_id', $user->id)->get()->groupBy('title');
Heres an example of a Group By Query:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
go with select() and please add your group by field in select
e.g DB::table('notes')->select('title','Other_field')->where('user_id', $user->id)->groupBy('title')->get();

retrieving multiple columns from table using redbeanphp

I have a query which retrieves 3 columns from table which works fine in phpmyadmin
SELECT cab_id,
SUM(IF(rating=1,1,0)) as up,
SUM(IF(rating=0,1,0)) as down
FROM rating WHERE cab_id=101
table->'rating'
Can anyone help me to find how I can get this query worked using redbeanphp ?
I tried ,
R::getRow(),R::getAll(),R::$adapter->getAssoc()
none is working!!!
To check the correctness of code I tried,
SELECT * FROM rating group by cab_id having cab_id=101
and found working.But I need the first query statement to work! Any help ,please?
I got the answer finally, This one works.
$val = R::getAll($query);

SELECT * FROM games WHERE

I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')

Using HeidiSQL with mysql select statement with "not like" does not work

Ok guys, here's the story, I have a very simple query I'm trying to do and for the life of me I don't understand why it doesn't work.
I'm am using the streaming twitter feed to populate my database using keywords. Now I want to filter out the retweets by using this query
select * from earthquake
where earthquake.Text not like '%RT%'
order by earthquake.Text
It returns "0 rows affected, 0 rows found", and yes there are thousands of retweets, so I know they exist.
I do the same query with the '%#%' and it finds thousands as expected, it almost seems that everything except 'RT' works.
Did you try NOT LIKE '%RT #%'?
Is it possible that common word xxxrtxxx always contains in your Text field?
I don't know anything about mysql specifically, but is there a function like MSSQL's PatIndex() that you could use instead of not like?
Alternatively you could try something like this: (dont know if your db supports this syntax)
SELECT * FROM Earthquake
WHERE CASE WHEN Text LIKE '%RT%' THEN 1 ELSE 0 END = 0
ORDER BY Text
I'd also advise checking your logic of looking for "RT" somewhere in the Text column to identify the stuff you don't want, because I'm surprised that "Not Like [expression]" apparently doesnt work.