Get user's primary email address mysql php best practices - mysql

I have a user table and a contact info table so that each user can have multiple phone numbers and multiple email addresses. Is there a standard way to set and retrieve their primary contact info?
"humans" table
ID | name
1 | John
2 | Joan
"human_contact" table
ID | contact_type | contact_info | user_id
1 | email | john#a.com | 1
2 | email | johnny#b.net | 1
"tickets" table
ID | human_id | event_date
1 | 1 | 2017-08-01
if John usually uses johnny#b.net, how do I mark that row as his favorite and retrieve that one for most queries?
SELECT *
FROM tickets
LEFT JOIN humans
ON tickets.human_id=humans.ID
LEFT JOIN human_contact
ON human_contact.human_id=humans.ID
WHERE tickets.ID='112'

You can use an extra coloumn named as fav which will have boolean values. This can help you in first finding whether he has any fav using a check where fav==true, then going on with that contact_info , or if the user doesnt have any favourites, then go with your usual query.
That is,
if( query( fav==1 for atleast 1 tuple )){
return tuple with fav==1;
else{
query ( usual query to get contact_info );
return query_result;
}
for more easier understanding.Hope this helps.

Related

Left outer join with multiple matches, how to return a specific one?

I have a problem which I think might be solved with proper use of left outer join, but I'm unable to construct suitable query. OTOH, there may also be some other, more clever solution with SQL. In addition, this could easily be solved with some programming, but I want to avoid that and find as "clean" solution as possible.
Background: let's say I'm creating a website that lists some car brands and the user can select which ones he owns/has owned (I'm not really doing that, but this example illustrates the point). In addition, for the selected ones he can optionally enter some additional info about them, e.g. year and some free text like specific model, comments or whatever. In addition, the information entered is stored in a relational database (MySQL in my case) and the user can retrieve and change his answers later.
Let's say there are two database tables:
BRAND
------------
ID INT
NAME VARCHAR(50)
OWNED
------------
ID INT
BRAND_ID INT
OWNER_ID INT
YEAR INT
COMMENT VARCHAR(100)
(here BRAND_ID + OWNER_ID is an unique index, so there can be only one row, and thus one year & comment for each BRAND/OWNER combination)
The data in these tables may look something like this:
BRAND
--------------
ID | NAME
--------------
1 | Cadillac
2 | Chevrolet
3 | Dodge
4 | Ford
OWNED
-----------------------------------------
ID | BRAND_ID | OWNER_ID | YEAR | COMMENT
-----------------------------------------
1 | 1 | 1 | null | 70's Fleetwood
2 | 2 | 1 | 2000 | Crappy car
3 | 2 | 2 | null | I really liked it
4 | 4 | 2 | 1999 | null
Now, to facilitate easy creation of the web page, what I would like to do is to with one SELECT display all brands in table BRAND, and for each BRAND to know whether current user has owned it or not, and if he has, also list his year and comment (if any). In other words, something like this (assuming current user is 2):
NAME | OWNER_ID | YEAR | COMMENT
-------------------------------------
Cadillac | null | null | null
Chevrolet | 2 | null | I really liked it
Dodge | null | null | null
Ford | 2 | 1999 | null
I tried doing something like:
select NAME, OWNER_ID, YEAR, COMMENT from BRAND left join OWNED on
BRAND.ID = OWNED.BRAND_ID where OWNER_ID = 2 or OWNER_ID = null
but that fails because 1 owns a Cadillac and thus Cadillac is left from the result. OTOH if I omit the where clause, I will get two rows for Chevrolet, which is also not desirable.
So, if there is a clean solution with SQL (either with or without left outer join), I'd like to know how to do it?
I am guessing you want this:
select NAME, OWNER_ID, YEAR, COMMENT
from BRAND left join
OWNED o
on BRAND.ID = OWNED.BRAND_ID and OWNER_ID = 2 ;
Seems like what you might actually want is a list of the owners, followed by what they owned and the details. You can that by adjusting the owner id at the bottom of this one:
SELECT owned.owner_id, brand.id, brand.name, owned.year, owned.comment
FROM owned
INNER JOIN brand
ON owned.brand_id = brand.id
WHERE owned.owner_id = 2
Tested here: http://sqlfiddle.com/#!9/5b52ba

Select userids from mysql table if user has at least 3 fields filled

I have a mysql user table that holds user data like that:
userid | title | content
----------------------------------
1 | about | I am from ...
1 | location | Norway
1 | name | Mark
1 | website |
2 | about |
2 | location |
2 | name |
2 | website |
3 | ...
As you see the content is empty for userid 2, and also for many more users in the table.
My goal is to select only the userids that have at least 3 fields filled. All others should be ignored.
As my mysql knowledge is still weak I could not find a solution for this. I only found the opposite and just with count: Find the count of EMPTY or NULL columns in a MySQL table
What is the magic mysql query? Any help appreciated, thank you.
You would use aggregation and a having clause for this:
select u.userId
from users u
where content > '' and content is not null
group by u.userId
having count(*) >= 3;
I added the non-blank check as well as the null check. The null check is redundant, but it makes the intention clearer.

Search for rows containing regex with my matchstring

My users enter a regular expression of URLs in the app. I have to return the rows with regular expressions that matched given url.
I have a table like this:
+----+-----------------------+--------------------------+
| id | Survey | URL Regexp |
+----+-----------------------+--------------------------+
| 1 | User Age survey | http://*.google.com/* |
| 2 | Payment intent survey | http://mail.google.com/* |
| 3 | User Country info | *reader.google.com* |
| 4 | Company information | http://facebook.com/* |
| 5 | Satisfaction survey | http://*twitter.com/* |
+----+-----------------------+--------------------------+
I want to run a query against a url, http://mail.google.com/index.html and see (1, 2) as a result (i.e. User Age Survey and Payment intent survey records).
I want a run a query like this:
SELECT * FROM surveys where MATCH_REGEXP('http://mail.google.com/index.html')
I wasn't able to find any documentation for such expression. What would be the best way to solve this?
Here's a working sqlfiddle example.
Given you have this data:
CREATE TABLE surveys (
id int auto_increment primary key,
survey varchar(30),
url_regex varchar(30)
);
INSERT INTO surveys (survey, url_regex) VALUES
('User Age survey', 'http://.*.google.com/*'),
('Payment intent survey', 'http://mail.google.com/*'),
('User Country info', '*reader.google.com*'),
('Company information', 'http://facebook.com/*'),
('Satisfaction survey', 'http://*twitter.com/*');
You can do this query to achieve what you want:
SELECT id FROM surveys
WHERE 'http://mail.google.com/index.html' REGEXP url_regex;
The result will be:
1
2
Please, note that in order to achieve desired result, i.e. (1, 2), "User Age survey" regular expression had to be fixed by prepending a dot before the first asterisk.

Delete users with no posts in WordPress using SQL query

I need to write a SQL script to delete users with no posts associated with them in a WordPress database. I tried this script after doing some searching:
DELETE FROM wp_users WHERE ID NOT IN (SELECT DISTINCT post_author FROM wp_105_posts)
but this deleted all the users. Can someone let me know what I am doing wrong please?
Seems like either none of the users made posts, or wp_105_posts.post_author doesn't link to wp_users.ID. Are you sure wp_105_posts.post_author is a Foreign Key of wp_users.ID?
wp_105_posts should have one column that references who the user is in the wp_users table. Since your GUID Primary Key for wp_users is ID, this should be the one column you use to reference the user from your post tables. Here is a small example:
user_table
ID | name | age | email | etc.
1 | nick | 24 | nick#com.com | ..
2 | bob | 30 | bob#com.com | ...
3 | sue | 35 | sue#com.com | ...
a_post_table
ID (ID of post) | User_ID (ID of user) | title | date | body
........1..............|...1 (post by nick)........ | help | 1/1/11
........2..............|...1 (post by nick)........ | help | 3/2/11
........3..............|...2 (post by bob)........ | help | 5/6/11
As you can see, the post table knows everything about the author by simply holding its ID. You can use a join query to get all user info from the post table just by knowing its ID. Using this query now
DELETE FROM user_table
WHERE ID NOT IN
(SELECT DISTINCT User_ID FROM a_post_table)
Will delete Sue, and Sue only, given the data above.
HTH

MYSQL INSERT SELECT problem

i have a little difficulty in understanding how to do some INSERT SELECT.
For instance i have two tables.
TABLE : users
id | name | gender
1 | John | m
2 | Mary | f
TABLE : website
fid | url | id
1 | www.desilva.biz | 2
2 | gidhelp.com | 4
Now let's say i want to add another query to the table website. I get two variables, lets say:
$user = John;
$site = "www.google.com";
i want to select the id of John from users table and insert it into website table in one statement.
How can i do it?
Assuming your variables are already escaped properly and are not subject to SQL injection:
INSERT
INTO website (url, fid)
SELECT $site, id
FROM users
WHERE name = $user