retrieving multiple columns from table using redbeanphp - mysql

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

Related

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

Multiple check in same column in 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

Why this query and condition return rows

I'm working on a large select statement, it was working fine, but with some dates
it gives a strange result, I started reducing it to track the mistake in vain!
until I wrote this code
SELECT DISTINCT
CURDATE() AS `Rdat`,
`p`.`pspsalm1` AS `pspsalm1`,
`p`.`pspsalm2` AS `pspsalm2`,
`p`.`pspsalm3` AS `pspsalm3`,
p.`PSDescription` AS `PSDescription`
FROM
(`dailyreadings` `d`, `psalms` `p`, `weeks` `w` ,`feasts` `f`)
WHERE 1=2
it gives result as well..!
I think this condition ( 1=2) should give no result
any clarification?
It shouldn't give any result
Try:
refreshing your browser
clear broser cache and try again
Thanks to you guys
It is a browser issue,
the browser kept the previous criteria and query data using it, even when I changed the condition in the sql statement.
Thanks to you all

Concatenate a string with curdate in mysql

I have the following query:
INSERT INTO insertlog (Inforamtion) VALUES (
concat("Row Was Inserted",curdate());
MySQL is returning an error, but I cannot figure out why. My google searches do not show examples on how to perform something like this.
use it simpler like that
INSERT INTO insertlog (Inforamtion)
SELECT concat("Row Was Inserted ",curdate()) ;
be sure if your column is Information or Inforamtion
your query also works but you missed ) in the end . here demo with both solutions :
Demo to try here
I think you are missing one closing )
Moreover as Bill pointed, you may have spelled your column name incorrectly - information

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