Select 2 tables from db and get combined results in a table - mysql

database table 1 has 3 rows. with ids id 1, 2, 3
and database table 2 has 2 rows, with ids IDUnique 4, 5 (table 2 is already generating the IDUnique next after last table 1 id)
In my html table data, I want to show information combined from those 2 tables, like so:
table:
1. user | name | pass // from table 1
2. user | name | pass // from table 1
3. user | name | pass // from table 1
4. user | name | pass // from table 2
5. user | name | pass // from table 2
What php query should i use to select the 2 tables simoultanesly?
And how it should be the while loop used in html table for that?

A simple UNION will suffice
SELECT user,name,pass FROM table_1_name
UNION
SELECT user,name,pass FROM table_2_name;
For unique dataset irrespective of two tables, use UNION ALL instead of UNION

Related

MySQL - how to get count of a single item frequency in a table of CSV values

I have a mysql table called "projects" with a single field containing CSV lists of project Ids. Assume that I cannot change the table structure.
I need a query that will allow me to quickly retrieve a count of rows that contain a particular project id, for example:
select count(*) from projects where '4' in (project_ids);
This returns just 1 result, which is incorrect (should be 3 results), but I think that it illustrates what I'm attempting to do.
CREATE TABLE `projects` (
`project_ids` varchar(255) DEFAULT NULL
);
INSERT INTO `projects` (`project_ids`)
VALUES
('1,2,4'),
('1,2'),
('4'),
('4,5,2'),
('1,2,5');
I was hoping that there might be a simple mysql function that would achieve this so that I don't have to anything complex sql-wise.
You could use this approach:
SELECT COUNT(*)
FROM projects
WHERE CONCAT(',', project_ids, ',') LIKE '%,4,%';
Or use FIND_IN_SET for a built-in way:
SELECT COUNT(*)
FROM projects
WHERE FIND_IN_SET('4', project_ids) > 0;
But, as to that which Gordon's comment alludes, a much better table design would be to have a junction table which relates a primary key in one table to all projects in another table. That junction table, based off your sample data, would look like this:
PK | project_id
1 | 1
1 | 2
1 | 4
2 | 1
2 | 2
3 | 4
4 | 4
4 | 5
4 | 2
5 | 1
5 | 2
5 | 5
With this design, if you wanted to find the count of PK's having a project_id of 4, you would only need a much simpler (and sargable) query:
SELECT COUNT(*)
FROM junction_table
WHERE project_id = 4;
You would need to use a like condition as follows
select count(*)
from projects
where concat(',',project_ids,',') like '%,4,%';

MySQL, Inserting multiple row from string separated by comma

I understand that I can use the value in insert into to insert multiple rows by columns but my case is a bit different:
I have a string that looks like this: "2,5,14,25,30".
And a connection table that has 2 columns: (INT UserId, INT GroupId).
Let's say I want to connect userId = 1 with all the IDs in the string, how should I do that?
I am familiar with the FIND_IN_SET() function that helps me search for those IDs, but I can't figure how to use it in this case.
In the end, I want my table to look like this:
UserId | GroupId
-----------------
1 | 2
1 | 5
1 | 14
1 | 25
1 | 30

add two table per row based on its name

I have two tables and i want to add there row based on its name. I've search on net but I only found how to combine the total value or two tables and combine them. The result will be added on a table named Result
Table 1 Table 2 Result
Name | Value Name | Value Name | Value
Apple | 2 Apple | 4 Apple | 6
Orange | 3 Orange | 2 Orange| 5
Thank you in advance
First of all I would like to say you must try to get solution at your own.
For your case, answer is very simple. Try this query :
SELECT table1.`name`, (table2.value + table1.value) AS `value` FROM table1
LEFT JOIN table2 ON table1.`name` = table2.`name` WHERE table1.`name` = table2.`name`

export phpList subscribers via sql in mysql database

For some reason, I am unable to export a table of subscribers from my phpList (ver. 3.0.6) admin pages. I've searched on the web, and several others have had this problem but no workarounds have been posted. As a workaround, I would like to query the mySQL database directly to retrieve a similar table of subscribers. But I need help with the SQL command. Note that I don't want to export or backup the mySQL database, I want to query it in the same way that the "export subscribers" button is supposed to do in the phpList admin pages.
In brief, I have two tables to query. The first table, user contains an ID and email for every subscriber. For example:
id | email
1 | e1#gmail.com
2 | e2#gmail.com
The second table, user_attribute contains a userid, attributeid, and value. Note in the example below that userid 1 has values for all three possible attributes, while userid's 2 and 3 are either missing one or more of the three attributeid's, or have blank values for some.
userid | attributeid | value
1 | 1 | 1
1 | 2 | 4
1 | 3 | 6
2 | 1 | 3
2 | 3 |
3 | 1 | 4
I would like to execute a SQL statement that would produce a row of output for each id/email that would look like this (using id 3 as an example):
id | email | attribute1 | attribute2 | attribute3
3 | e3#gmail.com | 4 | "" | "" |
Can someone suggest SQL query language that could accomplish this task?
A related query I would like to run is to find all id/email that do not have a value for attribute3. In the example above, this would be id's 2 and 3. Note that id 3 does not even have a blank value for attributeid3, it is simply missing.
Any help would be appreciated.
John
I know this is a very old post, but I just had to do the same thing. Here's the query I used. Note that you'll need to modify the query based on the custom attributes you have setup. You can see I had name, city and state as shown in the AS clauses below. You'll need to map those to the attribute id. Also, the state has a table of state names that I linked to. I excluded blacklisted (unsubscribed), more than 2 bounces and unconfirmed users.
SELECT
users.email,
(SELECT value
FROM `phplist_user_user_attribute` attrs
WHERE
attrs.userid = users.id and
attributeid=1
) AS name,
(SELECT value
FROM `phplist_user_user_attribute` attrs
WHERE
attrs.userid = users.id and
attributeid=3
) AS city,
(SELECT st.name
FROM `phplist_user_user_attribute` attrs
LEFT JOIN `phplist_listattr_state` st
ON attrs.value = st.id
WHERE
attrs.userid = users.id and
attributeid=4
) AS state
FROM
`phplist_user_user` users
WHERE
users.blacklisted=0 and
users.bouncecount<3 and
users.confirmed=1
;
I hope someone finds this helpful.

Selecting from two joined tables where one column of a table is referred by two from the other one

I have two tables, Page and Hyperlink to store Web page URLs and their connections(hyperlinks) respectively. In the Page table, I have a page_id column that is AUTO_INCREMENT for all unique entries. In the Hyperlink table, I use the page_id for both source and destination for the hyperlink.
Table: Page
page_id | page_url
--------------------
1 | a.com
2 | b.com
3 | c.com
4 | d.com
Table: Hyperlink
hyperlink_id | source_id | destination_id
1 | 1 | 2
2 | 1 | 3
3 | 2 | 4
4 | 4 | 4
I want to retrieve a ResultSet that returns me two Strings in a row, a.com and b.com, given the hyperlink_id. Let's assume the hyperlink_id is 1.
I tried using the query below but it didn't seem to work.
SELECT Page.page_url, Page.page_url from Hyperlink
JOIN Page
ON Page.page_id = Hyperlink.source_id
AND Page.page_id = Hyperlink.destination_id
where Hyperlink.hyperlink_id = 1
This query returned me an empty ResultSet.
However, this query worked for hyperlink_id = 4. I am thinking this query doesn't work when source_id != destination_id. What is wrong with my SELECT statement?
Your query can be re-written as:
SELECT Page.page_url, Page.page_url
FROM Hyperlink
JOIN Page
ON Page.page_id = Hyperlink.source_id
AND Hyperlink.source_id = Hyperlink.destination_id
WHERE Hyperlink.hyperlink_id = 1
This implies that Hyperlink.source_id must equal Hyperlink.destination_id and you only have one row in your table that fulfills this condition. The one where the hyperlink_id = 4.
Maybe you meant the following instead?
SELECT p1.page_url, p2.page_url
FROM Hyperlink
JOIN Page p1
ON p1.page_id = Hyperlink.source_id
JOIN Page p2
AND p2.page_id = Hyperlink.destination_id
WHERE Hyperlink.hyperlink_id = 1
This will access the Page table twice to get the information you need for the two different IDs