I have a Card Regions which display some of my tables. I want the cards to each redirect to a certain page based on a number (the number labelled has "page_table" is the page number it will redirect on).
Here's the Query of the Cards Region
SELECT table_name,
REPLACE(SUBSTR(table_name, 8),'_','-') AS nom_table, -- here, i remove the "INV_TB_" prefix from my tables
CASE
WHEN table_name = 'INV_TB_BATIMENT' THEN 11
WHEN table_name = 'INV_TB_CATEGORIE' THEN 12
WHEN table_name = 'INV_TB_DEPARTEMENT' THEN 13
WHEN table_name = 'INV_TB_SERVICE' THEN 14
WHEN table_name = 'INV_TB_POSSEDER' THEN 15
WHEN table_name = 'INV_TB_SITUER' THEN 16
WHEN table_name = 'INV_TB_ETAGE_BUREAU' THEN 16 END AS page_table --this is the column where i define the page number for each table
from cat
WHERE table_name LIKE 'INV_TB_%'
AND table_name <> 'INV_TB_AMORTISSEMENT'
AND table_name <> 'INV_TB_ARTICLE_LOGS'
AND table_name <> 'INV_TB_ARTICLE_DASHBOARD'
AND table_name <> 'INV_TB_DOCUMENT'
AND table_name <> 'INV_TB_VILLE'
AND table_name <> 'INV_TB_DASHBOARD'
AND table_name <> 'INV_TB_ARTICLE'
Here's the result :
For each one of them, I want to be able to retrieve the number in the little square on the left :
Which will be used to redirect the user to the specific page in Link and Target of the Cards :
I've thought about using an Hidden Item which will receive the value of the square when I click on the card, maybe using JavaScript or jQuery ? Or is there a more easy way to retrieve the value of the custom column "page_table" based on the card I'm clicking on ?
My last solution is to create a table which will contains the name of the real tables, with the page number in real columns, but I would like to know first if my first idea is possible
Thanks in advance,
Thomas
In the cards region, the substitution syntax for columns is &COLUMN_NAME. so in your case &PAGE_TABLE. should work. Can you give that a try ?
Related
I want to make a primitive personalized feed in a NodeJS+MySQL website. Now I just select all the posts that have specific tags in them:
SELECT column1, column2...
FROM table
WHERE tags = users_tags
ORDER BY relavance
LIMIT 8;
I want to also throw in a couple of popular posts eg:
SELECT column1, column2...
FROM table
ORDER BY relevance
LIMIT 2;
I don't want to use UNION because I want to retain the ordering from my first select and insert a popular result for every 5th post. Eg.: relevant, relevant, relevant, relevant, popular, relevant...
For now, I've results1.concat(results2) which adds them as the last two, and returned it. Then I had a for loop that would append the to HTML normally for the first 4 and then one from the back for every 5th.
Is there any better solution?
What you could use, is row_number to define the sortorder
SELECT column1,column2
FROM
(SELECT column1,column2,IF(rn = 1, B,D) as sortorder
FROM
(SELECT column1, column2, ROW_NUMBER() OVER(ORDER BY relavance) rn
FROM table1
ORDER BY relavance
LIMIT 2) t1
UNION
SELECT column1,column2,IF(rn < 5, A,C) as sortorder
FROM
(SELECT column1, column2, ROW_NUMBER() OVER(ORDER BY relavance) rn
FROM table1
WHERE tags = users_tags
ORDER BY relavance
LIMIT 8) t2) t3
ORDER BY sortorder
You can use ROW_NUMBER() window function to rank separately the posts with specific tags and the popular posts and then do a conditional sort:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY tags = users_tags ORDER BY relevance) rn
FROM tablename
)
SELECT *
FROM cte
ORDER BY CASE
WHEN tags = users_tags THEN rn + FLOOR((rn - 1) / 4)
ELSE (4 + 1) * rn
END
LIMIT 10;
Change 4 to the number of posts with specific tags that you want in each block.
See a simplified demo.
I have made a gravityform risk assessment quiz. Depending on answers, assigns respondent a profile: aggressive, balanced, conservative. I have 3 MySQL tables with names corresponding to each of those profiles.
This is a single query for 1 of the 3 tables:
$query= "
SELECT value1
, value3
, value4
FROM aggressive
order
by num
LIMIT 7,8
";
This is the gravityform field showing “aggressive, balanced or conservative”:
$profile = slideval($_POST["input_31"]);
How do I write the MySQL query so it selects the right profile and display result in 1 single HTML page, instead of having to create 3 different HTML pages with 3 different queries?
Thanks a lot!
$profile = slideval($_POST["input_31"]);
$query= "SELECT value1,value3,value4 FROM $profile order by num LIMIT 7,8";
You can go with union or unionall
$query = “
SELECT value1,value3,value4 , ‘aggressive’ As tablename FROM aggressive
UNION
SELECT value1,value3,value4 , ‘aggressive’ As tablename FROM balanced
UNION
SELECT value1,value3,value4 , ‘aggressive’ As tablename FROM conservative”;
I tried to search for an answer to this but I'm not sure if there are any answers to this question because I'm not quite sure how to word it correctly... Here I have two tables
Recipe ingredient table
Recipe_id|ingredient_id
ifqvv |1
ifqvv |2
User ingredient table
User_id|ingredient_id
1 |1
1 |2
2 |1
3 |3
I need to compare these table to where if both recipe and user ingredients_id are a complete match it will return a 1, or if the ingredients are greater than 0 it'll return a 2, and 3 for no matches. For Example, a query for User 1 will return a 1, User 2 a 2, and User 3 a 3. I'm not sure if this is something I'll have to code but I was told by someone that this is possible with little information, which led me here
Assuming you also have a user table, you could achieve this by cross-joining the user with the recipe_ingredients table and then left-joining the user_ingredients table, e.g.:
SELECT u.user_id, ri.recipe_id,
COUNT(ui.ingredient_id) AS available_ingredients, -- Number of ingredients the user has that are required to cook this recipe
COUNT(ri.ingredient_id) AS required_ingredients, -- Number of ingredients that are required to cook this recipe
CASE
WHEN COUNT(ui.ingredient_id) = COUNT(ri.ingredient_id) THEN 'can_cook'
WHEN COUNT(ui.ingredient_id) > 0 THEN 'has_some_ingredients'
ELSE 'has_no_ingredients'
END AS state
FROM users u
CROSS JOIN recipe_ingredients ri
LEFT JOIN user_ingredients ui ON(ri.ingredient_id = ui.ingredient_id AND u.user_id = ui.user_id)
GROUP BY u.user_id, ri.recipe_id
ORDER BY u.user_id, ri.recipe_id
If you want to limit it to a certain user / recipe, just use a where clause:
WHERE u.user_id = 1 AND ri.recipe_id = 'ifqvv'
You can try it live here: DB Fiddle
If you don't have an users table then you can replace
FROM users u with FROM (SELECT DISTINCT user_id FROM user_ingredients) u
DB Fiddle
I have more than 50 tables that are not related but all of them have a "Name" field, I want to query "John" and get all Johns in the different tables and store each row in an array (Javascript) for example:
arr['table1']="results if any"
arr["table2"]="results if any".
What I'm doing right now is a for loop for each table:
SELECT * from tablesNameArray[i] WHERE name="John",
but I'm really wondering if there is any other better or "more" correct way to do it.
Thanks
You can do it in a single query using UNION:
SELECT * FROM table1 WHERE name = 'John'
UNION ALL
SELECT * FROM table2 WHERE name = 'John'
UNION ALL
SELECT * FROM table3 WHERE name = 'John'
...
You can construct the query dynamically from the array:
sql = tablesNameArray.map(table => `SELECT * FROM ${table} WHERE name = 'John'`).join(" SELECT ALL ");
You could use joins. See the join docs, MySQL in this case.
Something like this should do it:
SELECT table1.*, table2.*, table3.*
FROM table1
LEFT JOIN table2 ON table1.name = table2.name
LEFT JOIN table3 ON table1.name = table3.name
WHERE table1.name = "John";
A left join will still select from the first table even if the joined table doesn't have a matching row -- you'll get NULL in the second table's selected columns for those rows which didn't match.
However, depending on your requirements, this is possibly no good -- I believe it won't grab rows from table2 or table3 where there's no corresponding row for that user in table1.
As pointed out by #spencer7593 in a comment below, you should probably only do this if you're certain the name column is unique within each table, otherwise you could be generating some ridiculously huge result sets.
I have the following database structure:
id (int) | user_id (int) | product_id (int) | data (jsonb)
A combination of the id, user_id and product_id make the primary key. So there can be multiple rows with the same product_id and user_id.
The data column has some JSON containing the following
{ "item": boolean }
The query I need is to select all rows where user_id = x and data-->item = true. This part I can do, but I need to apply a limit. The limit should not restrict the number of rows that are returned, but instead restrict the number of DISTINCT product_ids that are returned. So if I apply a limit of 10 I could have 50 rows returned if each of the unique products have 5 rows belonging to the user_id and and item true.
This is what I have so far but it makes no attempt at this limit. I believe I may need a subquery or GROUPBY but I'm not sure how to achieve this in Sequalize.
return this.myModel.findAll({
where: {
user_id: userId,
'data.item': true,
},
});
Any guidance will be much appreciated! Thanks
A query to do this involves JOINing a subquery:
SELECT m.*
FROM my_model m
JOIN (
SELECT DISTINCT product_id FROM model LIMIT 10
) n ON n.product_id = m.product_id
WHERE m.user_id = $1 AND (data->>'item')::boolean IS TRUE;
To my knowledge, Sequelize cannot represent this query structure, although inlining the subquery as a literal may be possible. But it looks like you'll be running at least some raw SQL one way or the other.