A little help in my Mysql statement - mysql

If there's a sql select statement that get the last value of the data.
I mean like this:
database table 1
|id| code| name |
|1 | abc | absent |
|2 | cbd | tabsent|
|3 | def | late |
|4 | efg | kalant |
how to get the data with the end value of ent:
SELECT * FROM table 1
WHERE (endValue of name)= "ent"

SELECT *
FROM Table1
WHERE name like "%ent"

SELECT *
FROM Table1
WHERE RIGHT(name, 3) = "ent"
would also be a possibility, instead of a LIKE "%ent" statement. See http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right

Related

Sort table by matching record percentage

I have a table with parent_id and percentage_score which is a result of a simple deduplication. My problem now is how can I select the records and arrange them like this:
|id|lastname|firstname|number|parent_id|percentage_score|
--------------------------------------------------------
|1 |holmes | james |11112 | 0 |100% |
|7 |holmes | john |11112 | 1 |80% |
|8 |holmes | james |11113 | 1 |60% |
|4 |sherlock| steve |77777 | 0 |100% |
|9 |sherlock| stove |77777 | 4 |80% |
|12|ketchum | ash |00001 | 0 |100% |
I tried using order by parent_id, percentage_score but that's not even near to what I want.
Your request is confusing because your expected result does not seem to be in a logical order. But, you can use the DESC command to change the priority in the ORDER by clause, like this:
SELECT id, lastname, firstname, number, parent_id, percentage_score
ORDER BY parent_id, percentage_score DESC

Use result of a rails where query for another query

I'm curious if there is an easy way to archive this query:
SomeTable.where(value_b: SomeTable.where(value_a: 1).pluck(:value_b))
For an example, if we have:
SomeTable
|id | a | b |
|1 | 1 | 2 |
|2 | 2 | 2 |
|3 | 3 | 3 |
|4 | 4 | 4 |
And the user enter number 1 into the system, I want to be able to find value of b where value of a = 1, which is 2. Then use this value of b to find all the records with this value.
So, I want to be able to use 1 and extract SomeTable with id: 1 and 2
Thanks!
SomeTable.joins("JOIN some_table as replica on replica.value_b = some_table.value_b")
.where(replica: {value_a: 1})
SomeTable.where("b in (SELECT b FROM some_tables where a = ?)", a_value)
a_value is your 'a' value to find 'b' value

Counting whole DB while searching for specific SQL

I have a table in db for customers and their glasses
customer_inventory_tbl:
SELECT * FROM customer_inventory_tbl
+-------+-------+-------+
|id(pk) | name | spex |
+-------+-------+-------+
|1 |John |Oval |
|2 |Steve |Angular|
|3 |John |Aviator|
|4 |Kevin |Supra |
|5 |Jamie |Oval |
|6 |Ben |Supra |
+-------+-------+-------+
(this is a way more simplified version, haha)
If I view John's record it shows
SELECT * FROM customer_inventory_tbl WHERE name=John
+-------+-------+-------+
|id(pk) | name | spex |
+-------+-------+-------+
|1 |John |Oval |
|3 |John |Aviator|
+-------+-------+-------+
But what I require is when viewing John's record, it to show me
+-------+-------+-------+-----+
|id(pk) | name | spex |count|
+-------+-------+-------+-----+
|1 |John |Oval |2 |
|3 |John |Aviator|1 |
+-------+-------+-------+-----+
That "count" column is the number of records in the database that has "Oval" for instance.
Now that is easy enough, if I wanted to count every record in the db, but how do I get the count of all records whilst looking for a specific name.
I hope this makes sense
select c.*,
(
select count(1)
from customer_inventory_tbl
where spex = c.spex
) "count"
from customer_inventory_tbl c;
As a solution according to above mentioned description please try executing following sql query
SELECT *,(select count(id) from customer_inventory_tbl group by spex)
as count FROM customer_inventory_tbl WHERE name='John'
In above mentioned sql query counter value is being retrieved through subquery with records grouped according to values of spex column using GROUP BY clause.

Rewriting a select query

I have a rather simple (I think) question at hand. The example tables and the result I need are provided below (in reality those tables containt much more columns and data, I jest left what is relevant). There is also the query which returns exactly what I need. However, I dont like rather crude way in which it works (I dont like subqueries in general). The question is, how can I rewrite the query so it will automatically react to more columns appearing in TABLE2 in the future? Right now if the "z" column would be added to TABLE2, I need to modify each query in the code and add one more relevant subquery. I just want the select to read the entire content of TABLE2 and translate the id numbers to corresponding strings from TABLE1.
TABLE1
-----------------
id |x |
-----------------
567 |AAA |
345 |BBB |
341 |CCC |
827 |DDD |
632 |EEE |
503 |FFF |
945 |GGG |
234 |HHH |
764 |III |
123 |JJJ |
-----------------
TABLE2
-------------------------
id |x |y |
-------------------------
1 |123 |341 |
2 |567 |632 |
3 |345 |945 |
4 |764 |503 |
5 |234 |827 |
-------------------------
THE RESULT I NEED
-----------------
A |B |
-----------------
JJJ |CCC |
AAA |EEE |
BBB |GGG |
III |FFF |
HHH |DDD |
-----------------
The query I have
SELECT
(SELECT `x` FROM `TABLE1` WHERE `TABLE2`.`x` LIKE `TABLE1`.`id` LIMIT 1) as A,
(SELECT `x` FROM `TABLE1` WHERE `TABLE2`.`y` LIKE `TABLE1`.`id` LIMIT 1) as B
FROM `TABLE2` ORDER BY `id` DESC;
You might want to restructure your data model:
Instead of:
-------------------------
id |x |y |
-------------------------
1 |123 |341 |
2 |567 |632 |
3 |345 |945 |
4 |764 |503 |
5 |234 |827 |
-------------------------
You would have:
----------------------
col_id |col |
----------------------
1 |x |
2 |y |
----------------------
---------------------------
id |col_id |col_val |
---------------------------
1 |1 |123 |
1 |2 |341 |
2 |1 |567 |
2 |2 |632 |
etc
---------------------------
Probably not worth the hassle (you would effectively need to pivot when you're accessing multiple columns at a time) but it would allow you to do the query that you want across all current and future columns.
You can't do that with a plain select.
What you can do is creating a view with the translated values. You still have to modify the view when the original table is changed but your queries don't have to.
You can use dynamic sql statements, but still you can use the dynamic statements only if you are sure that table 2 will have the columns of same type like x and y(Apart from id).
Let me know if you are not sure how to write it.
All the best.

multicount on SQl query

I am looking for the proper SQl query to pull data from a the database and COUNT the specific rows to come up with a total... here's my table:
------------------------------------------
|name |App |Dep |Sold |
------------------------------------------
|Joe |1 |1 |2 |
|Joe |1 |2 |2 |
|Steve |1 |1 |1 |
|Steve |1 |2 |1 |
------------------------------------------
So I need to count the "1" in each column for each name and come up and output the totals like this:
Joe | 2 App | 1 Dep | 0 Sold
Steve | 2 App | 1 Dep | 2 Sold
Anyone have a starting point for me? I'm not sure if i need JOINs or i can just add seperate COUNTs for each column?
SELECT Name,
SUM(App = 1) TotalApp,
SUM(Dep = 1) TotalDep,
SUM(Sold = 1) TotalSold
FROM tableName
GROUP BY Name
SQLFiddle Demo
App = 1 is a mysql specific syntax which performs boolean arithmetic resulting 1 and 0. To make it more RDBMS friendly, you can use CASE eg. SUM(CASE WHEN App = 1 THEN 1 ELSE 0 END).
SQL Fiddle Demo using CASE statement